2021-07-10 23:41:21 +03:00
|
|
|
// Package kgo provides a kafka broker using kgo
|
|
|
|
package kgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-09 00:30:36 +03:00
|
|
|
"fmt"
|
2021-08-22 12:10:58 +03:00
|
|
|
"math/rand"
|
2021-08-09 00:30:36 +03:00
|
|
|
"strings"
|
2021-07-10 23:41:21 +03:00
|
|
|
"sync"
|
2021-08-24 13:20:31 +03:00
|
|
|
"sync/atomic"
|
2021-07-10 23:41:21 +03:00
|
|
|
"time"
|
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
kerr "github.com/twmb/franz-go/pkg/kerr"
|
2021-07-10 23:41:21 +03:00
|
|
|
kgo "github.com/twmb/franz-go/pkg/kgo"
|
2021-08-27 02:24:43 +03:00
|
|
|
kmsg "github.com/twmb/franz-go/pkg/kmsg"
|
2021-07-10 23:41:21 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
2021-08-09 00:30:36 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2021-08-22 12:10:58 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/id"
|
2021-08-24 13:20:31 +03:00
|
|
|
mrand "github.com/unistack-org/micro/v3/util/rand"
|
2021-08-22 12:10:58 +03:00
|
|
|
"golang.org/x/sync/errgroup"
|
2021-07-10 23:41:21 +03:00
|
|
|
)
|
|
|
|
|
2021-08-26 00:36:32 +03:00
|
|
|
var pPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
2021-08-27 02:24:43 +03:00
|
|
|
return &publication{msg: &broker.Message{}}
|
2021-08-26 00:36:32 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
type kBroker struct {
|
2021-08-09 00:30:36 +03:00
|
|
|
writer *kgo.Client // used only to push messages
|
|
|
|
kopts []kgo.Opt
|
2021-07-10 23:41:21 +03:00
|
|
|
connected bool
|
|
|
|
init bool
|
|
|
|
sync.RWMutex
|
|
|
|
opts broker.Options
|
2021-08-09 00:30:36 +03:00
|
|
|
subs []*subscriber
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber struct {
|
2021-08-09 00:30:36 +03:00
|
|
|
reader *kgo.Client // used only to pull messages
|
|
|
|
topic string
|
|
|
|
opts broker.SubscribeOptions
|
|
|
|
kopts broker.Options
|
|
|
|
handler broker.Handler
|
|
|
|
batchhandler broker.BatchHandler
|
|
|
|
closed bool
|
|
|
|
done chan struct{}
|
2021-07-10 23:41:21 +03:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type publication struct {
|
2021-08-09 00:30:36 +03:00
|
|
|
topic string
|
|
|
|
err error
|
2021-07-10 23:41:21 +03:00
|
|
|
sync.RWMutex
|
|
|
|
msg *broker.Message
|
2021-08-22 12:10:58 +03:00
|
|
|
ack bool
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 13:20:31 +03:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
func (p *publication) Topic() string {
|
|
|
|
return p.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Message() *broker.Message {
|
|
|
|
return p.msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Ack() error {
|
2021-08-22 12:10:58 +03:00
|
|
|
p.ack = true
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publication) Error() error {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
func (p *publication) SetError(err error) {
|
|
|
|
p.err = err
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
func (s *subscriber) Options() broker.SubscribeOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) Unsubscribe(ctx context.Context) error {
|
2021-08-24 13:20:31 +03:00
|
|
|
if s.closed {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2021-08-24 13:20:31 +03:00
|
|
|
close(s.done)
|
|
|
|
s.closed = true
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Address() string {
|
2021-08-09 00:30:36 +03:00
|
|
|
return strings.Join(k.opts.Addrs, ",")
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Name() string {
|
|
|
|
return k.opts.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Connect(ctx context.Context) error {
|
|
|
|
k.RLock()
|
|
|
|
if k.connected {
|
|
|
|
k.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
k.RUnlock()
|
|
|
|
|
|
|
|
nctx := k.opts.Context
|
|
|
|
if ctx != nil {
|
|
|
|
nctx = ctx
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:10:58 +03:00
|
|
|
kaddrs := k.opts.Addrs
|
|
|
|
|
|
|
|
// shuffle addrs
|
|
|
|
rand.Shuffle(len(kaddrs), func(i, j int) {
|
|
|
|
kaddrs[i], kaddrs[j] = kaddrs[j], kaddrs[i]
|
|
|
|
})
|
|
|
|
|
|
|
|
kopts := append(k.kopts, kgo.SeedBrokers(kaddrs...))
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
select {
|
|
|
|
case <-nctx.Done():
|
|
|
|
return nctx.Err()
|
|
|
|
default:
|
2021-08-22 12:10:58 +03:00
|
|
|
c, err := kgo.NewClient(kopts...)
|
2021-07-10 23:41:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
k.Lock()
|
|
|
|
k.connected = true
|
|
|
|
k.writer = c
|
|
|
|
k.Unlock()
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Disconnect(ctx context.Context) error {
|
|
|
|
k.RLock()
|
|
|
|
if !k.connected {
|
|
|
|
k.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
k.RUnlock()
|
|
|
|
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
|
|
|
nctx := k.opts.Context
|
|
|
|
if ctx != nil {
|
|
|
|
nctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-nctx.Done():
|
|
|
|
return nctx.Err()
|
|
|
|
default:
|
2021-08-09 00:30:36 +03:00
|
|
|
for _, sub := range k.subs {
|
|
|
|
if err := sub.Unsubscribe(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
k.writer.Close()
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
k.connected = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Init(opts ...broker.Option) error {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
|
|
|
if len(opts) == 0 && k.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&k.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := k.opts.Register.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := k.opts.Tracer.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := k.opts.Logger.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := k.opts.Meter.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
if k.opts.Context != nil {
|
|
|
|
if v, ok := k.opts.Context.Value(optionsKey{}).([]kgo.Opt); ok && len(v) > 0 {
|
2021-08-25 21:51:15 +03:00
|
|
|
k.kopts = append(k.kopts, v...)
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-25 21:51:15 +03:00
|
|
|
|
2021-08-24 13:20:31 +03:00
|
|
|
// kgo.RecordPartitioner(),
|
2021-08-09 00:30:36 +03:00
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
k.init = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) Options() broker.Options {
|
|
|
|
return k.opts
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
func (k *kBroker) BatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
|
|
|
return k.publish(ctx, msgs, opts...)
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
func (k *kBroker) Publish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
2021-08-09 00:30:36 +03:00
|
|
|
msg.Header.Set(metadata.HeaderTopic, topic)
|
|
|
|
return k.publish(ctx, []*broker.Message{msg}, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kBroker) publish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
2021-08-25 21:51:15 +03:00
|
|
|
options := broker.NewPublishOptions(opts...)
|
2021-08-09 00:30:36 +03:00
|
|
|
records := make([]*kgo.Record, 0, len(msgs))
|
|
|
|
var errs []string
|
2021-08-25 21:51:15 +03:00
|
|
|
var err error
|
|
|
|
var buf []byte
|
2021-08-22 12:10:58 +03:00
|
|
|
|
|
|
|
for _, msg := range msgs {
|
2021-08-25 21:51:15 +03:00
|
|
|
if options.BodyOnly {
|
|
|
|
buf = msg.Body
|
|
|
|
} else {
|
|
|
|
buf, err = k.opts.Codec.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
topic, _ := msg.Header.Get(metadata.HeaderTopic)
|
2021-08-25 21:51:15 +03:00
|
|
|
rec := &kgo.Record{Value: buf, Topic: topic}
|
2021-08-22 12:10:58 +03:00
|
|
|
records = append(records, rec)
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
results := k.writer.ProduceSync(ctx, records...)
|
|
|
|
for _, result := range results {
|
|
|
|
if result.Err != nil {
|
|
|
|
errs = append(errs, result.Err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("publish error: %s", strings.Join(errs, "\n"))
|
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
type mlogger struct {
|
|
|
|
l logger.Logger
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mlogger) Log(lvl kgo.LogLevel, msg string, args ...interface{}) {
|
2021-08-24 13:20:31 +03:00
|
|
|
var mlvl logger.Level
|
2021-08-09 00:30:36 +03:00
|
|
|
switch lvl {
|
|
|
|
case kgo.LogLevelNone:
|
|
|
|
return
|
|
|
|
case kgo.LogLevelError:
|
|
|
|
mlvl = logger.ErrorLevel
|
|
|
|
case kgo.LogLevelWarn:
|
|
|
|
mlvl = logger.WarnLevel
|
|
|
|
case kgo.LogLevelInfo:
|
|
|
|
mlvl = logger.InfoLevel
|
|
|
|
case kgo.LogLevelDebug:
|
|
|
|
mlvl = logger.DebugLevel
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2021-08-25 21:51:15 +03:00
|
|
|
fields := make(map[string]interface{}, int(len(args)/2))
|
|
|
|
for i := 0; i < len(args)/2; i += 2 {
|
|
|
|
fields[fmt.Sprintf("%v", args[i])] = args[i+1]
|
|
|
|
}
|
|
|
|
l.l.Fields(fields).Log(l.ctx, mlvl, msg)
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mlogger) Level() kgo.LogLevel {
|
|
|
|
switch l.l.Options().Level {
|
|
|
|
case logger.ErrorLevel:
|
|
|
|
return kgo.LogLevelError
|
|
|
|
case logger.WarnLevel:
|
|
|
|
return kgo.LogLevelWarn
|
|
|
|
case logger.InfoLevel:
|
|
|
|
return kgo.LogLevelInfo
|
|
|
|
case logger.DebugLevel, logger.TraceLevel:
|
|
|
|
return kgo.LogLevelDebug
|
|
|
|
}
|
|
|
|
return kgo.LogLevelNone
|
|
|
|
}
|
|
|
|
|
2021-08-25 21:51:15 +03:00
|
|
|
func (k *kBroker) BatchSubscribe(ctx context.Context, topic string, handler broker.BatchHandler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
func (k *kBroker) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
|
|
options := broker.NewSubscribeOptions(opts...)
|
2021-08-09 00:30:36 +03:00
|
|
|
|
|
|
|
if options.Group == "" {
|
2021-08-22 12:10:58 +03:00
|
|
|
uid, err := id.New()
|
2021-08-09 00:30:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
options.Group = uid
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
|
|
|
|
2021-08-22 12:10:58 +03:00
|
|
|
kaddrs := k.opts.Addrs
|
|
|
|
|
|
|
|
// shuffle addrs
|
|
|
|
rand.Shuffle(len(kaddrs), func(i, j int) {
|
|
|
|
kaddrs[i], kaddrs[j] = kaddrs[j], kaddrs[i]
|
|
|
|
})
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
kopts := append(k.kopts,
|
2021-08-22 12:10:58 +03:00
|
|
|
kgo.SeedBrokers(kaddrs...),
|
2021-08-09 00:30:36 +03:00
|
|
|
kgo.ConsumerGroup(options.Group),
|
|
|
|
kgo.ConsumeTopics(topic),
|
|
|
|
kgo.ConsumeResetOffset(kgo.NewOffset().AtStart()),
|
|
|
|
kgo.DisableAutoCommit(),
|
2021-08-22 12:10:58 +03:00
|
|
|
kgo.FetchMaxWait(1*time.Second),
|
2021-08-27 02:24:43 +03:00
|
|
|
// kgo.KeepControlRecords(),
|
2021-08-24 13:20:31 +03:00
|
|
|
kgo.Balancers(kgo.CooperativeStickyBalancer(), kgo.StickyBalancer()),
|
|
|
|
kgo.FetchIsolationLevel(kgo.ReadUncommitted()),
|
2021-08-30 20:02:32 +03:00
|
|
|
kgo.WithHooks(&metrics{meter: k.opts.Meter}),
|
2021-08-24 13:20:31 +03:00
|
|
|
// TODO: must set https://pkg.go.dev/github.com/twmb/franz-go/pkg/kgo#OnRevoked
|
2021-08-09 00:30:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
reader, err := kgo.NewClient(kopts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
sub := &subscriber{topic: topic, done: make(chan struct{}), opts: options, reader: reader, handler: handler, kopts: k.opts}
|
2021-08-09 00:30:36 +03:00
|
|
|
go sub.run(ctx)
|
|
|
|
|
|
|
|
k.Lock()
|
|
|
|
k.subs = append(k.subs, sub)
|
|
|
|
k.Unlock()
|
2021-07-10 23:41:21 +03:00
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
func (s *subscriber) run(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-s.kopts.Context.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
fetches := s.reader.PollFetches(ctx)
|
|
|
|
if fetches.IsClientClosed() {
|
2021-08-24 13:20:31 +03:00
|
|
|
// TODO: fatal ?
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(fetches.Errors()) > 0 {
|
|
|
|
for _, err := range fetches.Errors() {
|
|
|
|
s.kopts.Logger.Errorf(ctx, "fetch err topic %s partition %d: %v", err.Topic, err.Partition, err.Err)
|
|
|
|
}
|
|
|
|
// TODO: fatal ?
|
2021-08-09 00:30:36 +03:00
|
|
|
return
|
|
|
|
}
|
2021-08-24 13:20:31 +03:00
|
|
|
|
|
|
|
if err := s.handleFetches(ctx, fetches); err != nil {
|
|
|
|
s.kopts.Logger.Errorf(ctx, "fetch handler err: %v", err)
|
|
|
|
// TODO: fatal ?
|
2021-08-25 21:51:15 +03:00
|
|
|
// return
|
2021-08-24 13:20:31 +03:00
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *subscriber) handleFetches(ctx context.Context, fetches kgo.Fetches) error {
|
|
|
|
var err error
|
|
|
|
|
2021-08-22 12:10:58 +03:00
|
|
|
eh := s.kopts.ErrorHandler
|
|
|
|
if s.opts.ErrorHandler != nil {
|
|
|
|
eh = s.opts.ErrorHandler
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:20:31 +03:00
|
|
|
var mu sync.Mutex
|
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
done := int32(0)
|
|
|
|
doneCh := make(chan struct{})
|
2021-08-24 13:20:31 +03:00
|
|
|
g, gctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
td := DefaultCommitInterval
|
|
|
|
if s.kopts.Context != nil {
|
|
|
|
if v, ok := s.kopts.Context.Value(commitIntervalKey{}).(time.Duration); ok && v > 0 {
|
|
|
|
td = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
// ticker for commit offsets
|
2021-08-24 13:20:31 +03:00
|
|
|
ticker := time.NewTicker(td)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
offsets := make(map[string]map[int32]kgo.EpochOffset)
|
|
|
|
offsets[s.topic] = make(map[int32]kgo.EpochOffset)
|
|
|
|
|
|
|
|
fillOffsets := func(off map[string]map[int32]kgo.EpochOffset, rec *kgo.Record) {
|
|
|
|
mu.Lock()
|
|
|
|
if at, ok := off[s.topic][rec.Partition]; ok {
|
|
|
|
if at.Epoch > rec.LeaderEpoch || at.Epoch == rec.LeaderEpoch && at.Offset > rec.Offset {
|
|
|
|
mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
off[s.topic][rec.Partition] = kgo.EpochOffset{Epoch: rec.LeaderEpoch, Offset: rec.Offset + 1}
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
commitOffsets := func(cl *kgo.Client, ctx context.Context, off map[string]map[int32]kgo.EpochOffset) error {
|
|
|
|
var rerr error
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
offsets := off
|
|
|
|
mu.Unlock()
|
|
|
|
|
|
|
|
cl.CommitOffsetsSync(ctx, offsets, func(_ *kgo.Client, _ *kmsg.OffsetCommitRequest, resp *kmsg.OffsetCommitResponse, err error) {
|
|
|
|
if err != nil {
|
|
|
|
rerr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, topic := range resp.Topics {
|
|
|
|
for _, partition := range topic.Partitions {
|
|
|
|
if err := kerr.ErrorForCode(partition.ErrorCode); err != nil {
|
|
|
|
rerr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2021-08-24 13:20:31 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-gctx.Done():
|
2021-08-27 02:24:43 +03:00
|
|
|
return
|
2021-08-24 13:20:31 +03:00
|
|
|
case <-s.done:
|
2021-08-27 02:24:43 +03:00
|
|
|
atomic.StoreInt32(&done, 1)
|
|
|
|
if err := commitOffsets(s.reader, ctx, offsets); err != nil && s.kopts.Logger.V(logger.ErrorLevel) {
|
|
|
|
s.kopts.Logger.Errorf(s.kopts.Context, "[kgo]: failed to commit offsets: %v", err)
|
2021-08-24 13:20:31 +03:00
|
|
|
}
|
2021-08-27 02:24:43 +03:00
|
|
|
return
|
|
|
|
case <-doneCh:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
if err := commitOffsets(s.reader, ctx, offsets); err != nil {
|
|
|
|
if s.kopts.Logger.V(logger.ErrorLevel) {
|
|
|
|
s.kopts.Logger.Errorf(s.kopts.Context, "[kgo]: failed to commit offsets: %v", err)
|
|
|
|
}
|
|
|
|
return
|
2021-08-24 13:20:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 02:24:43 +03:00
|
|
|
}()
|
2021-08-22 12:10:58 +03:00
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
for _, fetch := range fetches {
|
|
|
|
for _, ftopic := range fetch.Topics {
|
|
|
|
for _, partition := range ftopic.Partitions {
|
|
|
|
precords := partition.Records
|
|
|
|
g.Go(func() error {
|
|
|
|
for _, record := range precords {
|
|
|
|
if atomic.LoadInt32(&done) == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p := pPool.Get().(*publication)
|
|
|
|
p.msg.Header = nil
|
|
|
|
p.msg.Body = nil
|
|
|
|
p.topic = s.topic
|
|
|
|
p.err = nil
|
|
|
|
p.ack = false
|
|
|
|
if s.opts.BodyOnly {
|
2021-08-24 13:20:31 +03:00
|
|
|
p.msg.Body = record.Value
|
2021-08-27 02:24:43 +03:00
|
|
|
} else {
|
|
|
|
if err := s.kopts.Codec.Unmarshal(record.Value, p.msg); err != nil {
|
|
|
|
p.err = err
|
|
|
|
p.msg.Body = record.Value
|
|
|
|
if eh != nil {
|
|
|
|
_ = eh(p)
|
|
|
|
if p.ack {
|
|
|
|
fillOffsets(offsets, record)
|
|
|
|
}
|
|
|
|
pPool.Put(p)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if s.kopts.Logger.V(logger.ErrorLevel) {
|
|
|
|
s.kopts.Logger.Errorf(s.kopts.Context, "[kgo]: failed to unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pPool.Put(p)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = s.handler(p)
|
|
|
|
if err == nil && s.opts.AutoAck {
|
|
|
|
p.ack = true
|
|
|
|
} else if err != nil {
|
|
|
|
p.err = err
|
2021-08-24 13:20:31 +03:00
|
|
|
if eh != nil {
|
|
|
|
_ = eh(p)
|
|
|
|
} else {
|
|
|
|
if s.kopts.Logger.V(logger.ErrorLevel) {
|
2021-08-27 02:24:43 +03:00
|
|
|
s.kopts.Logger.Errorf(s.kopts.Context, "[kgo]: subscriber error: %v", err)
|
2021-08-24 13:20:31 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
2021-08-24 13:20:31 +03:00
|
|
|
if p.ack {
|
2021-08-27 02:24:43 +03:00
|
|
|
fillOffsets(offsets, record)
|
2021-08-22 12:10:58 +03:00
|
|
|
}
|
2021-08-27 02:24:43 +03:00
|
|
|
pPool.Put(p)
|
2021-08-22 12:10:58 +03:00
|
|
|
}
|
2021-08-27 02:24:43 +03:00
|
|
|
return nil
|
|
|
|
})
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
2021-08-27 02:24:43 +03:00
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
close(doneCh)
|
2021-08-22 12:10:58 +03:00
|
|
|
|
2021-08-27 02:24:43 +03:00
|
|
|
return commitOffsets(s.reader, ctx, offsets)
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
func (k *kBroker) String() string {
|
|
|
|
return "kgo"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBroker(opts ...broker.Option) broker.Broker {
|
2021-08-25 21:51:15 +03:00
|
|
|
options := broker.NewOptions(opts...)
|
|
|
|
kopts := []kgo.Opt{
|
2021-08-26 00:36:32 +03:00
|
|
|
kgo.BatchCompression(kgo.NoCompression()),
|
2021-08-25 21:51:15 +03:00
|
|
|
kgo.WithLogger(&mlogger{l: options.Logger, ctx: options.Context}),
|
|
|
|
kgo.RetryBackoffFn(
|
|
|
|
func() func(int) time.Duration {
|
|
|
|
var rng mrand.Rand
|
|
|
|
return func(fails int) time.Duration {
|
|
|
|
const (
|
|
|
|
min = 250 * time.Millisecond
|
|
|
|
max = 2 * time.Second
|
|
|
|
)
|
|
|
|
if fails <= 0 {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if fails > 10 {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
backoff := min * time.Duration(1<<(fails-1))
|
|
|
|
jitter := 0.8 + 0.4*rng.Float64()
|
|
|
|
backoff = time.Duration(float64(backoff) * jitter)
|
|
|
|
|
|
|
|
if backoff > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return backoff
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(optionsKey{}).([]kgo.Opt); ok && len(v) > 0 {
|
|
|
|
kopts = append(kopts, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
return &kBroker{
|
2021-08-25 21:51:15 +03:00
|
|
|
opts: options,
|
|
|
|
kopts: kopts,
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
}
|