This commit is contained in:
29
internal/queue/queue.go
Normal file
29
internal/queue/queue.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/segmentio/kafka-go"
|
||||
)
|
||||
|
||||
type Producer struct{ w *kafka.Writer }
|
||||
|
||||
func NewProducer(brokers []string, topic string) *Producer {
|
||||
return &Producer{w: &kafka.Writer{Addr: kafka.TCP(brokers...), Topic: topic, Balancer: &kafka.Hash{}, BatchSize: 100, BatchTimeout: 10 * time.Millisecond, RequiredAcks: kafka.RequireAll, Async: false}}
|
||||
}
|
||||
func (p *Producer) Write(ctx context.Context, key string, value []byte) error {
|
||||
return p.w.WriteMessages(ctx, kafka.Message{Key: []byte(key), Value: value, Time: time.Now().UTC()})
|
||||
}
|
||||
func (p *Producer) Close() error { return p.w.Close() }
|
||||
|
||||
type Consumer struct{ r *kafka.Reader }
|
||||
|
||||
func NewConsumer(brokers []string, topic, group string) *Consumer {
|
||||
return &Consumer{r: kafka.NewReader(kafka.ReaderConfig{Brokers: brokers, Topic: topic, GroupID: group, MinBytes: 1e3, MaxBytes: 16e6, MaxWait: 500 * time.Millisecond, CommitInterval: 0})}
|
||||
}
|
||||
func (c *Consumer) Fetch(ctx context.Context) (kafka.Message, error) { return c.r.FetchMessage(ctx) }
|
||||
func (c *Consumer) Commit(ctx context.Context, m kafka.Message) error {
|
||||
return c.r.CommitMessages(ctx, m)
|
||||
}
|
||||
func (c *Consumer) Close() error { return c.r.Close() }
|
||||
Reference in New Issue
Block a user