mirror of
https://github.com/go-eagle/eagle.git
synced 2025-09-27 04:45:58 +08:00

* feat: improve kafka component * chore: revert test password * docs: add changelog * chore: upgrade sarama to IBM/sarama
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package kafka
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/IBM/sarama"
|
|
)
|
|
|
|
// Publish add data to queue
|
|
func Publish(ctx context.Context, name string, topic, msg string) error {
|
|
p, err := DefaultManager.GetProducer(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return p.Publish(ctx, topic, msg)
|
|
}
|
|
|
|
// Consume data from queue
|
|
func Consume(ctx context.Context, name string, topics []string, handler sarama.ConsumerGroupHandler) error {
|
|
c, err := DefaultManager.GetConsumer(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Consume(ctx, topics, handler)
|
|
}
|
|
|
|
// ConsumeByPartition consume data by partition
|
|
func ConsumePartition(ctx context.Context, name, topic string, handler func([]byte) error) error {
|
|
c, err := DefaultManager.GetConsumer(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.ConsumePartition(ctx, topic, handler)
|
|
}
|
|
|
|
// ConsumerByPartitionId consume data by partition id
|
|
func ConsumerByPartitionId(ctx context.Context, name, topic string, partition int32, handler func([]byte) error) error {
|
|
c, err := DefaultManager.GetConsumer(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.ConsumerByPartitionId(ctx, topic, partition, handler)
|
|
}
|
|
|
|
// GetPartitionList get partition list
|
|
func GetPartitionList(ctx context.Context, name, topic string) ([]int32, error) {
|
|
c, err := DefaultManager.GetConsumer(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return c.client.Partitions(topic)
|
|
}
|