2020-04-08 11:53:02 +03:00
|
|
|
// Package kafka provides a kafka broker using segmentio
|
|
|
|
package segmentio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-19 15:53:47 +03:00
|
|
|
"fmt"
|
2020-04-08 11:53:02 +03:00
|
|
|
"sync"
|
2020-06-18 01:38:54 +03:00
|
|
|
"time"
|
2020-04-08 11:53:02 +03:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
kafka "github.com/segmentio/kafka-go"
|
2021-01-19 15:53:47 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-04-08 11:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type kBroker struct {
|
|
|
|
addrs []string
|
|
|
|
|
|
|
|
readerConfig kafka.ReaderConfig
|
|
|
|
writerConfig kafka.WriterConfig
|
|
|
|
|
|
|
|
writers map[string]*kafka.Writer
|
|
|
|
|
|
|
|
connected bool
|
|
|
|
sync.RWMutex
|
|
|
|
opts broker.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
2021-01-19 15:53:47 +03:00
|
|
|
k *kBroker
|
|
|
|
topic string
|
|
|
|
opts broker.SubscribeOptions
|
|
|
|
offset int64
|
|
|
|
gen *kafka.Generation
|
|
|
|
partition int
|
|
|
|
handler broker.Handler
|
|
|
|
reader *kafka.Reader
|
|
|
|
closed bool
|
|
|
|
done chan struct{}
|
|
|
|
group *kafka.ConsumerGroup
|
|
|
|
cgcfg kafka.ConsumerGroupConfig
|
|
|
|
brokerOpts broker.Options
|
2020-04-22 00:43:14 +03:00
|
|
|
sync.RWMutex
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type publication struct {
|
2020-04-22 00:43:14 +03:00
|
|
|
topic string
|
|
|
|
err error
|
|
|
|
m *broker.Message
|
2021-01-19 15:53:47 +03:00
|
|
|
opts broker.Options
|
2020-04-22 00:43:14 +03:00
|
|
|
ctx context.Context
|
|
|
|
generation *kafka.Generation
|
|
|
|
reader *kafka.Reader
|
|
|
|
km kafka.Message
|
|
|
|
offsets map[string]map[int]int64 // for commit offsets
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Topic() string {
|
2020-04-22 00:43:14 +03:00
|
|
|
return p.topic
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Message() *broker.Message {
|
|
|
|
return p.m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Ack() error {
|
2021-01-19 15:53:47 +03:00
|
|
|
if p.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
p.opts.Logger.Tracef(p.opts.Context, "commit offset %#+v\n", p.offsets)
|
2020-04-22 13:50:18 +03:00
|
|
|
}
|
2020-04-22 00:43:14 +03:00
|
|
|
return p.generation.CommitOffsets(p.offsets)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Error() error {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Options() broker.SubscribeOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
2020-04-22 00:43:14 +03:00
|
|
|
return s.topic
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
func (s *subscriber) Unsubscribe(ctx context.Context) error {
|
2020-04-22 00:43:14 +03:00
|
|
|
var err error
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
if s.group != nil {
|
|
|
|
err = s.group.Close()
|
|
|
|
}
|
2020-06-02 01:01:53 +03:00
|
|
|
s.closed = true
|
2020-04-22 00:43:14 +03:00
|
|
|
return err
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Address() string {
|
|
|
|
if len(k.addrs) > 0 {
|
|
|
|
return k.addrs[0]
|
|
|
|
}
|
|
|
|
return "127.0.0.1:9092"
|
|
|
|
}
|
|
|
|
|
2021-02-10 00:19:08 +03:00
|
|
|
func (k *kBroker) Name() string {
|
|
|
|
return k.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
func (k *kBroker) Connect(ctx context.Context) error {
|
2020-04-08 11:53:02 +03:00
|
|
|
k.RLock()
|
|
|
|
if k.connected {
|
|
|
|
k.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
k.RUnlock()
|
2021-01-19 15:53:47 +03:00
|
|
|
dialCtx := k.opts.Context
|
|
|
|
if ctx != nil {
|
|
|
|
dialCtx = ctx
|
|
|
|
}
|
2020-04-08 11:53:02 +03:00
|
|
|
kaddrs := make([]string, 0, len(k.addrs))
|
|
|
|
for _, addr := range k.addrs {
|
2021-01-19 15:53:47 +03:00
|
|
|
conn, err := kafka.DialContext(dialCtx, "tcp", addr)
|
2020-04-08 11:53:02 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err = conn.Brokers(); err != nil {
|
2021-01-19 15:53:47 +03:00
|
|
|
if err = conn.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 11:53:02 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
kaddrs = append(kaddrs, addr)
|
2021-01-19 15:53:47 +03:00
|
|
|
if err = conn.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(kaddrs) == 0 {
|
2021-01-19 15:53:47 +03:00
|
|
|
return fmt.Errorf("no available brokers: %v", k.addrs)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
k.Lock()
|
|
|
|
k.addrs = kaddrs
|
2020-04-13 17:56:40 +03:00
|
|
|
k.readerConfig.Brokers = k.addrs
|
|
|
|
k.writerConfig.Brokers = k.addrs
|
2020-04-08 11:53:02 +03:00
|
|
|
k.connected = true
|
|
|
|
k.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
func (k *kBroker) Disconnect(ctx context.Context) error {
|
2020-04-08 11:53:02 +03:00
|
|
|
k.RLock()
|
|
|
|
if !k.connected {
|
|
|
|
k.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
k.RUnlock()
|
|
|
|
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
for _, writer := range k.writers {
|
|
|
|
if err := writer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
k.connected = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Init(opts ...broker.Option) error {
|
2021-01-19 15:53:47 +03:00
|
|
|
return k.configure(opts...)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Options() broker.Options {
|
|
|
|
return k.opts
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
func (k *kBroker) Publish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
2020-06-18 01:38:54 +03:00
|
|
|
var cached bool
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
options := broker.NewPublishOptions(opts...)
|
|
|
|
|
|
|
|
val, err := k.opts.Codec.Marshal(msg)
|
2020-04-08 11:53:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-19 15:53:47 +03:00
|
|
|
kmsg := kafka.Message{Value: val}
|
|
|
|
if options.Context != nil {
|
|
|
|
if key, ok := options.Context.Value(publishKey{}).([]byte); ok && len(key) > 0 {
|
|
|
|
kmsg.Key = key
|
|
|
|
}
|
|
|
|
}
|
2020-06-18 01:38:54 +03:00
|
|
|
|
|
|
|
k.Lock()
|
|
|
|
writer, ok := k.writers[topic]
|
|
|
|
if !ok {
|
|
|
|
cfg := k.writerConfig
|
|
|
|
cfg.Topic = topic
|
|
|
|
if err = cfg.Validate(); err != nil {
|
|
|
|
k.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writer = kafka.NewWriter(cfg)
|
|
|
|
k.writers[topic] = writer
|
|
|
|
} else {
|
|
|
|
cached = true
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
2020-06-18 01:38:54 +03:00
|
|
|
k.Unlock()
|
2021-01-19 15:53:47 +03:00
|
|
|
wCtx := k.opts.Context
|
|
|
|
if ctx != nil {
|
|
|
|
wCtx = ctx
|
|
|
|
}
|
|
|
|
err = writer.WriteMessages(wCtx, kmsg)
|
2020-06-02 01:01:53 +03:00
|
|
|
if err != nil {
|
2021-01-20 22:16:59 +03:00
|
|
|
if k.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "write message err: %v", err)
|
|
|
|
}
|
2020-06-18 01:38:54 +03:00
|
|
|
switch cached {
|
|
|
|
case false:
|
|
|
|
// non cached case, we can try to wait on some errors, but not timeout
|
|
|
|
if kerr, ok := err.(kafka.Error); ok {
|
|
|
|
if kerr.Temporary() && !kerr.Timeout() {
|
|
|
|
// additional chanse to publish message
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
2021-01-19 15:53:47 +03:00
|
|
|
err = writer.WriteMessages(wCtx, kmsg)
|
2020-06-18 01:38:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case true:
|
|
|
|
// cached case, try to recreate writer and try again after that
|
|
|
|
k.Lock()
|
|
|
|
// close older writer to free memory
|
|
|
|
if err = writer.Close(); err != nil {
|
|
|
|
k.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2020-07-02 16:25:56 +03:00
|
|
|
delete(k.writers, topic)
|
|
|
|
k.Unlock()
|
|
|
|
|
2020-06-18 01:38:54 +03:00
|
|
|
cfg := k.writerConfig
|
|
|
|
cfg.Topic = topic
|
|
|
|
if err = cfg.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writer := kafka.NewWriter(cfg)
|
2021-01-19 15:53:47 +03:00
|
|
|
if err = writer.WriteMessages(wCtx, kmsg); err == nil {
|
2020-07-02 16:25:56 +03:00
|
|
|
k.Lock()
|
|
|
|
k.writers[topic] = writer
|
|
|
|
k.Unlock()
|
|
|
|
}
|
2020-06-18 01:38:54 +03:00
|
|
|
}
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 01:38:54 +03:00
|
|
|
return err
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
func (k *kBroker) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
|
|
opt := broker.NewSubscribeOptions(opts...)
|
|
|
|
|
|
|
|
if opt.Group == "" {
|
|
|
|
id, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opt.Group = id.String()
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 00:43:14 +03:00
|
|
|
cgcfg := kafka.ConsumerGroupConfig{
|
2021-01-19 15:53:47 +03:00
|
|
|
ID: opt.Group,
|
2020-04-13 15:51:23 +03:00
|
|
|
WatchPartitionChanges: true,
|
|
|
|
Brokers: k.readerConfig.Brokers,
|
|
|
|
Topics: []string{topic},
|
2020-04-21 18:35:24 +03:00
|
|
|
GroupBalancers: []kafka.GroupBalancer{kafka.RangeGroupBalancer{}},
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
2020-04-22 00:43:14 +03:00
|
|
|
if err := cgcfg.Validate(); err != nil {
|
2020-04-13 15:51:23 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-02 01:01:53 +03:00
|
|
|
cgroup, err := kafka.NewConsumerGroup(cgcfg)
|
2020-04-15 04:07:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
sub := &subscriber{brokerOpts: k.opts, opts: opt, topic: topic, group: cgroup, cgcfg: cgcfg}
|
2020-04-21 18:35:24 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-01-19 15:53:47 +03:00
|
|
|
case <-ctx.Done():
|
|
|
|
sub.RLock()
|
|
|
|
closed := sub.closed
|
|
|
|
sub.RUnlock()
|
|
|
|
if closed {
|
|
|
|
// unsubcribed and closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// unexpected context closed
|
|
|
|
if k.opts.Context.Err() != nil {
|
|
|
|
if k.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "[segmentio] context closed unexpected %v", k.opts.Context.Err())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2020-04-21 18:35:24 +03:00
|
|
|
case <-k.opts.Context.Done():
|
2020-06-02 01:01:53 +03:00
|
|
|
sub.RLock()
|
|
|
|
closed := sub.closed
|
|
|
|
sub.RUnlock()
|
|
|
|
if closed {
|
|
|
|
// unsubcribed and closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// unexpected context closed
|
|
|
|
if k.opts.Context.Err() != nil {
|
2021-01-19 15:53:47 +03:00
|
|
|
if k.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "[segmentio] context closed unexpected %v", k.opts.Context.Err())
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
2020-04-22 00:43:14 +03:00
|
|
|
}
|
2020-04-21 18:35:24 +03:00
|
|
|
return
|
2020-04-22 00:43:14 +03:00
|
|
|
default:
|
2020-06-02 01:01:53 +03:00
|
|
|
sub.RLock()
|
|
|
|
group := sub.group
|
|
|
|
sub.RUnlock()
|
2021-01-19 15:53:47 +03:00
|
|
|
gCtx := k.opts.Context
|
|
|
|
if ctx != nil {
|
|
|
|
gCtx = ctx
|
|
|
|
}
|
|
|
|
generation, err := group.Next(gCtx)
|
2020-04-22 00:43:14 +03:00
|
|
|
switch err {
|
2020-06-02 01:01:53 +03:00
|
|
|
case nil:
|
|
|
|
// normal execution
|
2020-04-22 00:43:14 +03:00
|
|
|
case kafka.ErrGroupClosed:
|
2020-06-02 01:01:53 +03:00
|
|
|
sub.RLock()
|
|
|
|
closed := sub.closed
|
|
|
|
sub.RUnlock()
|
|
|
|
if !closed {
|
2021-01-19 15:53:47 +03:00
|
|
|
if k.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "[segmentio] recreate consumer group, as it closed %v", k.opts.Context.Err())
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
|
|
|
if err = group.Close(); err != nil {
|
2021-01-19 15:53:47 +03:00
|
|
|
if k.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "[segmentio] consumer group close error %v", err)
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
2021-01-19 15:53:47 +03:00
|
|
|
continue
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
2021-01-19 15:53:47 +03:00
|
|
|
sub.createGroup(gCtx)
|
2020-04-22 00:43:14 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
default:
|
2020-06-02 01:01:53 +03:00
|
|
|
sub.RLock()
|
|
|
|
closed := sub.closed
|
|
|
|
sub.RUnlock()
|
|
|
|
if !closed {
|
2021-01-19 15:53:47 +03:00
|
|
|
if k.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "[segmentio] recreate consumer group, as unexpected consumer error %v", err)
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = group.Close(); err != nil {
|
2021-01-19 15:53:47 +03:00
|
|
|
if k.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "[segmentio] consumer group close error %v", err)
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
2020-04-22 00:43:14 +03:00
|
|
|
}
|
2020-06-02 01:01:53 +03:00
|
|
|
sub.createGroup(k.opts.Context)
|
2020-04-22 00:43:14 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range cgcfg.Topics {
|
|
|
|
assignments := generation.Assignments[t]
|
|
|
|
for _, assignment := range assignments {
|
|
|
|
cfg := k.readerConfig
|
|
|
|
cfg.Topic = t
|
|
|
|
cfg.Partition = assignment.ID
|
|
|
|
cfg.GroupID = ""
|
2020-06-02 01:01:53 +03:00
|
|
|
// break reading
|
2020-04-22 13:50:18 +03:00
|
|
|
reader := kafka.NewReader(cfg)
|
2021-01-19 15:53:47 +03:00
|
|
|
if k.opts.Logger.V(logger.TraceLevel) {
|
|
|
|
k.opts.Logger.Tracef(k.opts.Context, "[segmentio] reader current offset: %v new offset: %v", reader.Offset(), assignment.Offset)
|
|
|
|
}
|
2020-04-22 13:50:18 +03:00
|
|
|
reader.SetOffset(assignment.Offset)
|
2021-01-19 15:53:47 +03:00
|
|
|
|
2020-04-22 13:50:18 +03:00
|
|
|
cgh := &cgHandler{generation: generation, brokerOpts: k.opts, subOpts: opt, reader: reader, handler: handler}
|
2020-04-22 00:43:14 +03:00
|
|
|
generation.Start(cgh.run)
|
|
|
|
}
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
2020-06-02 01:01:53 +03:00
|
|
|
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-04-13 15:51:23 +03:00
|
|
|
return sub, nil
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 00:43:14 +03:00
|
|
|
type cgHandler struct {
|
|
|
|
topic string
|
|
|
|
generation *kafka.Generation
|
|
|
|
brokerOpts broker.Options
|
|
|
|
subOpts broker.SubscribeOptions
|
|
|
|
reader *kafka.Reader
|
|
|
|
handler broker.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *cgHandler) run(ctx context.Context) {
|
|
|
|
offsets := make(map[string]map[int]int64)
|
|
|
|
offsets[h.reader.Config().Topic] = make(map[int]int64)
|
|
|
|
|
|
|
|
defer h.reader.Close()
|
2020-04-21 18:35:24 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2020-04-22 00:43:14 +03:00
|
|
|
msg, err := h.reader.ReadMessage(ctx)
|
2020-04-21 18:35:24 +03:00
|
|
|
switch err {
|
2020-04-22 00:43:14 +03:00
|
|
|
default:
|
2021-01-19 15:53:47 +03:00
|
|
|
if h.brokerOpts.Logger.V(logger.TraceLevel) {
|
|
|
|
h.brokerOpts.Logger.Tracef(h.brokerOpts.Context, "[segmentio] unexpected error: %v", err)
|
2020-04-22 00:43:14 +03:00
|
|
|
}
|
|
|
|
return
|
2020-04-21 18:35:24 +03:00
|
|
|
case kafka.ErrGenerationEnded:
|
|
|
|
// generation has ended
|
2021-01-19 15:53:47 +03:00
|
|
|
if h.brokerOpts.Logger.V(logger.TraceLevel) {
|
|
|
|
h.brokerOpts.Logger.Trace(h.brokerOpts.Context, "[segmentio] generation ended")
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
case nil:
|
2020-04-22 00:43:14 +03:00
|
|
|
eh := h.brokerOpts.ErrorHandler
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
if h.subOpts.ErrorHandler != nil {
|
|
|
|
eh = h.subOpts.ErrorHandler
|
|
|
|
}
|
|
|
|
offsets[msg.Topic][msg.Partition] = msg.Offset + 1
|
|
|
|
// github.com/segmentio/kafka-go/commit.go makeCommit builds commit message with offset + 1
|
|
|
|
// zookeeper store offset which needs to be sent on new consumer, so current + 1
|
|
|
|
p := &publication{topic: msg.Topic, opts: h.brokerOpts, generation: h.generation, m: &broker.Message{}, offsets: offsets}
|
|
|
|
|
|
|
|
if h.subOpts.BodyOnly {
|
2020-04-21 18:35:24 +03:00
|
|
|
p.m.Body = msg.Value
|
2021-01-19 15:53:47 +03:00
|
|
|
} else {
|
|
|
|
if err := h.brokerOpts.Codec.Unmarshal(msg.Value, p.m); err != nil {
|
|
|
|
p.err = err
|
|
|
|
p.m.Body = msg.Value
|
|
|
|
if eh != nil {
|
|
|
|
eh(p)
|
|
|
|
} else {
|
|
|
|
if h.brokerOpts.Logger.V(logger.ErrorLevel) {
|
|
|
|
h.brokerOpts.Logger.Errorf(h.brokerOpts.Context, "[segmentio]: failed to unmarshal: %v", err)
|
|
|
|
}
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
2021-01-19 15:53:47 +03:00
|
|
|
continue
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 00:43:14 +03:00
|
|
|
err = h.handler(p)
|
|
|
|
if err == nil && h.subOpts.AutoAck {
|
2020-04-21 18:35:24 +03:00
|
|
|
if err = p.Ack(); err != nil {
|
2021-01-19 15:53:47 +03:00
|
|
|
if h.brokerOpts.Logger.V(logger.ErrorLevel) {
|
|
|
|
h.brokerOpts.Logger.Errorf(h.brokerOpts.Context, "[segmentio]: unable to commit msg: %v", err)
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
p.err = err
|
|
|
|
if eh != nil {
|
|
|
|
eh(p)
|
|
|
|
} else {
|
2021-01-19 15:53:47 +03:00
|
|
|
if h.brokerOpts.Logger.V(logger.ErrorLevel) {
|
|
|
|
h.brokerOpts.Logger.Errorf(h.brokerOpts.Context, "[segmentio]: subscriber error: %v", err)
|
2020-04-21 18:35:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 00:43:14 +03:00
|
|
|
|
2020-06-02 01:01:53 +03:00
|
|
|
func (sub *subscriber) createGroup(ctx context.Context) {
|
|
|
|
sub.RLock()
|
|
|
|
cgcfg := sub.cgcfg
|
|
|
|
sub.RUnlock()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// closed
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
cgroup, err := kafka.NewConsumerGroup(cgcfg)
|
|
|
|
if err != nil {
|
2021-01-19 15:53:47 +03:00
|
|
|
if sub.brokerOpts.Logger.V(logger.ErrorLevel) {
|
|
|
|
sub.brokerOpts.Logger.Errorf(sub.brokerOpts.Context, "[segmentio]: consumer group error %v", err)
|
2020-06-02 01:01:53 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sub.Lock()
|
|
|
|
sub.group = cgroup
|
|
|
|
sub.Unlock()
|
|
|
|
// return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:53:02 +03:00
|
|
|
func (k *kBroker) String() string {
|
|
|
|
return "kafka"
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
func (k *kBroker) configure(opts ...broker.Option) error {
|
2020-04-08 11:53:02 +03:00
|
|
|
for _, o := range opts {
|
2021-01-19 15:53:47 +03:00
|
|
|
o(&k.opts)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cAddrs []string
|
2021-01-19 15:53:47 +03:00
|
|
|
for _, addr := range k.opts.Addrs {
|
2020-04-08 11:53:02 +03:00
|
|
|
if len(addr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cAddrs = append(cAddrs, addr)
|
|
|
|
}
|
|
|
|
if len(cAddrs) == 0 {
|
|
|
|
cAddrs = []string{"127.0.0.1:9092"}
|
|
|
|
}
|
|
|
|
|
|
|
|
readerConfig := kafka.ReaderConfig{}
|
2021-01-19 15:53:47 +03:00
|
|
|
if cfg, ok := k.opts.Context.Value(readerConfigKey{}).(kafka.ReaderConfig); ok {
|
2020-04-08 11:53:02 +03:00
|
|
|
readerConfig = cfg
|
|
|
|
}
|
|
|
|
if len(readerConfig.Brokers) == 0 {
|
|
|
|
readerConfig.Brokers = cAddrs
|
|
|
|
}
|
|
|
|
readerConfig.WatchPartitionChanges = true
|
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
writerConfig := kafka.WriterConfig{CompressionCodec: nil, BatchSize: 1}
|
|
|
|
if cfg, ok := k.opts.Context.Value(writerConfigKey{}).(kafka.WriterConfig); ok {
|
2020-04-08 11:53:02 +03:00
|
|
|
writerConfig = cfg
|
|
|
|
}
|
|
|
|
if len(writerConfig.Brokers) == 0 {
|
|
|
|
writerConfig.Brokers = cAddrs
|
|
|
|
}
|
2021-01-19 15:53:47 +03:00
|
|
|
k.addrs = cAddrs
|
|
|
|
k.writerConfig = writerConfig
|
|
|
|
k.readerConfig = readerConfig
|
2020-04-08 11:53:02 +03:00
|
|
|
|
2021-01-19 15:53:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBroker(opts ...broker.Option) broker.Broker {
|
2020-04-08 11:53:02 +03:00
|
|
|
return &kBroker{
|
2021-01-19 15:53:47 +03:00
|
|
|
writers: make(map[string]*kafka.Writer),
|
|
|
|
opts: broker.NewOptions(opts...),
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
}
|