Files
eagle/pkg/queue/kafka/client.go
Richard fb1b10bcf5 chore: improve kafka component (#180)
* feat: improve kafka component

* chore: revert test password

* docs: add changelog

* chore: upgrade sarama to IBM/sarama
2025-04-12 17:33:05 +08:00

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)
}