2021-07-10 23:41:21 +03:00
|
|
|
// Package kgo provides a kafka broker using kgo
|
2024-02-21 15:08:58 +03:00
|
|
|
package kgo
|
2021-07-10 23:41:21 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-13 21:56:44 +03:00
|
|
|
"errors"
|
2021-08-09 00:30:36 +03:00
|
|
|
"fmt"
|
2024-12-19 13:21:32 +03:00
|
|
|
"math/rand/v2"
|
2024-07-22 23:45:23 +03:00
|
|
|
"net/http"
|
2021-08-09 00:30:36 +03:00
|
|
|
"strings"
|
2021-07-10 23:41:21 +03:00
|
|
|
"sync"
|
2024-12-04 14:54:13 +03:00
|
|
|
"sync/atomic"
|
2021-07-10 23:41:21 +03:00
|
|
|
"time"
|
|
|
|
|
2024-02-21 15:08:58 +03:00
|
|
|
"github.com/google/uuid"
|
2023-03-13 21:56:44 +03:00
|
|
|
"github.com/twmb/franz-go/pkg/kgo"
|
2023-05-13 15:12:26 +03:00
|
|
|
"github.com/twmb/franz-go/pkg/kmsg"
|
2021-10-03 18:30:56 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
2024-04-13 00:40:12 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2021-10-03 18:30:56 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-04-13 02:24:16 +03:00
|
|
|
"go.unistack.org/micro/v3/semconv"
|
2024-02-21 15:08:58 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2021-10-03 18:30:56 +03:00
|
|
|
mrand "go.unistack.org/micro/v3/util/rand"
|
2021-07-10 23:41:21 +03:00
|
|
|
)
|
|
|
|
|
2023-05-13 15:12:26 +03:00
|
|
|
var _ broker.Broker = (*Broker)(nil)
|
2021-09-23 07:06:51 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
var ErrLostMessage = errors.New("message not marked for offsets commit and will be lost in next iteration")
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
var DefaultRetryBackoffFn = func() func(int) time.Duration {
|
|
|
|
var rngMu sync.Mutex
|
|
|
|
return func(fails int) time.Duration {
|
|
|
|
const (
|
|
|
|
min = 100 * time.Millisecond
|
|
|
|
max = time.Second
|
|
|
|
)
|
|
|
|
if fails <= 0 {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if fails > 10 {
|
|
|
|
return max
|
|
|
|
}
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
backoff := min * time.Duration(1<<(fails-1))
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
rngMu.Lock()
|
2024-12-19 13:21:32 +03:00
|
|
|
jitter := 0.8 + 0.4*rand.Float64()
|
2023-03-13 21:56:44 +03:00
|
|
|
rngMu.Unlock()
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
backoff = time.Duration(float64(backoff) * jitter)
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
if backoff > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return backoff
|
|
|
|
}
|
|
|
|
}()
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
type Broker struct {
|
2024-12-19 13:36:55 +03:00
|
|
|
c *kgo.Client
|
2024-12-04 14:54:13 +03:00
|
|
|
connected *atomic.Uint32
|
2024-12-19 13:21:32 +03:00
|
|
|
|
|
|
|
kopts []kgo.Opt
|
2024-12-19 13:36:55 +03:00
|
|
|
subs []*Subscriber
|
2024-12-19 13:21:32 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
opts broker.Options
|
2024-12-19 13:21:32 +03:00
|
|
|
|
|
|
|
sync.RWMutex
|
2024-12-19 13:36:55 +03:00
|
|
|
init bool
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
2024-12-04 14:54:13 +03:00
|
|
|
func (r *Broker) Live() bool {
|
|
|
|
return r.connected.Load() == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Broker) Ready() bool {
|
|
|
|
return r.connected.Load() == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Broker) Health() bool {
|
|
|
|
return r.connected.Load() == 1
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) Address() string {
|
|
|
|
return strings.Join(k.opts.Addrs, ",")
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) Name() string {
|
|
|
|
return k.opts.Name
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
2024-05-22 17:49:36 +03:00
|
|
|
func (k *Broker) Client() *kgo.Client {
|
|
|
|
return k.c
|
|
|
|
}
|
|
|
|
|
2024-05-06 07:30:17 +03:00
|
|
|
func (k *Broker) connect(ctx context.Context, opts ...kgo.Opt) (*kgo.Client, *hookTracer, error) {
|
2023-03-13 21:56:44 +03:00
|
|
|
var c *kgo.Client
|
|
|
|
var err error
|
2024-04-14 23:17:38 +03:00
|
|
|
|
|
|
|
sp, _ := tracer.SpanFromContext(ctx)
|
2024-02-21 15:08:58 +03:00
|
|
|
|
|
|
|
clientID := "kgo"
|
|
|
|
group := ""
|
|
|
|
if k.opts.Context != nil {
|
|
|
|
if id, ok := k.opts.Context.Value(clientIDKey{}).(string); ok {
|
|
|
|
clientID = id
|
|
|
|
}
|
|
|
|
if id, ok := k.opts.Context.Value(groupKey{}).(string); ok {
|
|
|
|
group = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 07:30:17 +03:00
|
|
|
htracer := &hookTracer{group: group, clientID: clientID, tracer: k.opts.Tracer}
|
2024-02-21 15:08:58 +03:00
|
|
|
opts = append(opts,
|
|
|
|
kgo.WithHooks(&hookMeter{meter: k.opts.Meter}),
|
2024-05-06 07:30:17 +03:00
|
|
|
kgo.WithHooks(htracer),
|
2024-02-21 15:08:58 +03:00
|
|
|
)
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-02-21 15:08:58 +03:00
|
|
|
if ctx.Err() != nil {
|
2024-05-02 10:32:29 +03:00
|
|
|
if sp != nil {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, ctx.Err().Error())
|
|
|
|
}
|
2024-02-21 15:08:58 +03:00
|
|
|
}
|
2024-05-06 07:30:17 +03:00
|
|
|
return nil, nil, ctx.Err()
|
2021-08-09 00:30:36 +03:00
|
|
|
default:
|
2023-03-13 21:56:44 +03:00
|
|
|
c, err = kgo.NewClient(opts...)
|
|
|
|
if err == nil {
|
|
|
|
err = c.Ping(ctx) // check connectivity to cluster
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-05-02 10:32:29 +03:00
|
|
|
if sp != nil {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
}
|
2024-05-06 07:30:17 +03:00
|
|
|
return nil, nil, err
|
2023-03-13 21:56:44 +03:00
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
2024-05-06 07:30:17 +03:00
|
|
|
return c, htracer, nil
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) Connect(ctx context.Context) error {
|
2024-12-04 14:54:13 +03:00
|
|
|
if k.connected.Load() == 1 {
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nctx := k.opts.Context
|
|
|
|
if ctx != nil {
|
|
|
|
nctx = ctx
|
|
|
|
}
|
|
|
|
|
2024-05-06 07:30:17 +03:00
|
|
|
c, _, err := k.connect(nctx, k.kopts...)
|
2023-03-13 21:56:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
k.Lock()
|
|
|
|
k.c = c
|
2024-12-04 14:54:13 +03:00
|
|
|
k.connected.Store(1)
|
2023-03-13 21:56:44 +03:00
|
|
|
k.Unlock()
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) Disconnect(ctx context.Context) error {
|
2024-12-04 14:54:13 +03:00
|
|
|
if k.connected.Load() == 0 {
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nctx := k.opts.Context
|
|
|
|
if ctx != nil {
|
|
|
|
nctx = ctx
|
|
|
|
}
|
2024-02-21 15:08:58 +03:00
|
|
|
var span tracer.Span
|
|
|
|
ctx, span = k.opts.Tracer.Start(ctx, "Disconnect")
|
|
|
|
defer span.Finish()
|
2021-07-10 23:41:21 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
2021-07-10 23:41:21 +03:00
|
|
|
select {
|
|
|
|
case <-nctx.Done():
|
|
|
|
return nctx.Err()
|
|
|
|
default:
|
2021-08-09 00:30:36 +03:00
|
|
|
for _, sub := range k.subs {
|
2023-05-13 15:12:26 +03:00
|
|
|
if sub.closed {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
if err := sub.Unsubscribe(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 21:56:44 +03:00
|
|
|
if k.c != nil {
|
|
|
|
k.c.CloseAllowingRebalance()
|
|
|
|
// k.c.Close()
|
|
|
|
}
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
|
2024-12-04 14:54:13 +03:00
|
|
|
k.connected.Store(0)
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) Init(opts ...broker.Option) error {
|
2021-07-10 23:41:21 +03:00
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
2024-03-07 09:06:33 +03:00
|
|
|
if len(opts) == 0 && k.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
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
|
|
|
|
2024-03-07 09:06:33 +03:00
|
|
|
k.init = true
|
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) Options() broker.Options {
|
2021-07-10 23:41:21 +03:00
|
|
|
return k.opts
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) BatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
2021-08-09 00:30:36 +03:00
|
|
|
return k.publish(ctx, msgs, opts...)
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) 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...)
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) publish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
2024-02-21 15:08:58 +03:00
|
|
|
k.Lock()
|
2024-12-04 14:54:13 +03:00
|
|
|
if k.connected.Load() == 0 {
|
2024-05-06 07:30:17 +03:00
|
|
|
c, _, err := k.connect(ctx, k.kopts...)
|
2023-03-13 21:56:44 +03:00
|
|
|
if err != nil {
|
|
|
|
k.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k.c = c
|
2024-12-04 14:54:13 +03:00
|
|
|
k.connected.Store(1)
|
2021-11-13 18:58:08 +03:00
|
|
|
}
|
2024-02-21 15:08:58 +03:00
|
|
|
k.Unlock()
|
2023-03-13 21:56:44 +03:00
|
|
|
|
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
|
2021-10-17 18:29:05 +03:00
|
|
|
var key []byte
|
2024-02-21 15:08:58 +03:00
|
|
|
var promise func(*kgo.Record, error)
|
2021-10-17 18:29:05 +03:00
|
|
|
|
|
|
|
if options.Context != nil {
|
|
|
|
if k, ok := options.Context.Value(publishKey{}).([]byte); ok && k != nil {
|
|
|
|
key = k
|
|
|
|
}
|
2024-02-21 15:08:58 +03:00
|
|
|
if p, ok := options.Context.Value(publishPromiseKey{}).(func(*kgo.Record, error)); ok && p != nil {
|
|
|
|
promise = p
|
|
|
|
}
|
2021-10-17 18:29:05 +03:00
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
|
|
|
|
for _, msg := range msgs {
|
2022-10-11 11:55:12 +03:00
|
|
|
rec := &kgo.Record{Context: ctx, Key: key}
|
2021-09-23 07:06:51 +03:00
|
|
|
rec.Topic, _ = msg.Header.Get(metadata.HeaderTopic)
|
2024-02-21 15:08:58 +03:00
|
|
|
msg.Header.Del(metadata.HeaderTopic)
|
2024-04-13 02:24:16 +03:00
|
|
|
k.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc()
|
2024-02-15 10:20:15 +03:00
|
|
|
if options.BodyOnly || k.opts.Codec.String() == "noop" {
|
2021-09-23 07:06:51 +03:00
|
|
|
rec.Value = msg.Body
|
|
|
|
for k, v := range msg.Header {
|
2024-07-22 23:45:23 +03:00
|
|
|
rec.Headers = append(rec.Headers, kgo.RecordHeader{Key: http.CanonicalHeaderKey(k), Value: []byte(v)})
|
2021-09-23 07:06:51 +03:00
|
|
|
}
|
2021-08-25 21:51:15 +03:00
|
|
|
} else {
|
2021-09-23 07:06:51 +03:00
|
|
|
rec.Value, err = k.opts.Codec.Marshal(msg)
|
2021-08-25 21:51:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 12:10:58 +03:00
|
|
|
records = append(records, rec)
|
|
|
|
}
|
|
|
|
|
2024-02-21 15:08:58 +03:00
|
|
|
if promise != nil {
|
2024-04-13 02:24:16 +03:00
|
|
|
ts := time.Now()
|
2024-02-21 15:08:58 +03:00
|
|
|
for _, rec := range records {
|
|
|
|
k.c.Produce(ctx, rec, func(r *kgo.Record, err error) {
|
2024-04-13 02:24:16 +03:00
|
|
|
te := time.Since(ts)
|
|
|
|
k.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Dec()
|
|
|
|
k.opts.Meter.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", rec.Topic, "topic", rec.Topic).Update(te.Seconds())
|
|
|
|
k.opts.Meter.Histogram(semconv.PublishMessageDurationSeconds, "endpoint", rec.Topic, "topic", rec.Topic).Update(te.Seconds())
|
2024-12-19 13:42:25 +03:00
|
|
|
if err != nil {
|
|
|
|
k.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", rec.Topic, "topic", rec.Topic, "status", "failure").Inc()
|
|
|
|
} else {
|
|
|
|
k.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", rec.Topic, "topic", rec.Topic, "status", "success").Inc()
|
|
|
|
}
|
2024-02-21 15:08:58 +03:00
|
|
|
promise(r, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-13 02:24:16 +03:00
|
|
|
ts := time.Now()
|
2023-03-13 21:56:44 +03:00
|
|
|
results := k.c.ProduceSync(ctx, records...)
|
2024-04-13 02:24:16 +03:00
|
|
|
te := time.Since(ts)
|
2021-08-09 00:30:36 +03:00
|
|
|
for _, result := range results {
|
2024-04-13 02:24:16 +03:00
|
|
|
k.opts.Meter.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
|
|
|
k.opts.Meter.Histogram(semconv.PublishMessageDurationSeconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
|
|
|
k.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Dec()
|
2021-08-09 00:30:36 +03:00
|
|
|
if result.Err != nil {
|
2024-04-13 02:24:16 +03:00
|
|
|
k.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "failure").Inc()
|
2021-08-09 00:30:36 +03:00
|
|
|
errs = append(errs, result.Err.Error())
|
2024-12-19 13:42:25 +03:00
|
|
|
} else {
|
|
|
|
k.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "success").Inc()
|
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
2021-10-17 18:29:05 +03:00
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-22 17:49:36 +03:00
|
|
|
func (k *Broker) TopicExists(ctx context.Context, topic string) error {
|
|
|
|
mdreq := kmsg.NewMetadataRequest()
|
|
|
|
mdreq.Topics = []kmsg.MetadataRequestTopic{
|
|
|
|
{Topic: &topic},
|
|
|
|
}
|
|
|
|
|
|
|
|
mdrsp, err := mdreq.RequestWith(ctx, k.c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if mdrsp.Topics[0].ErrorCode != 0 {
|
|
|
|
return fmt.Errorf("topic %s not exists or permission error", topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-19 13:21:32 +03:00
|
|
|
func (k *Broker) BatchSubscribe(_ context.Context, _ string, _ broker.BatchHandler, _ ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2021-08-25 21:51:15 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2021-07-10 23:41:21 +03:00
|
|
|
options := broker.NewSubscribeOptions(opts...)
|
2021-08-09 00:30:36 +03:00
|
|
|
|
|
|
|
if options.Group == "" {
|
2024-02-21 15:08:58 +03:00
|
|
|
uid, err := uuid.NewRandom()
|
2021-08-09 00:30:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-21 15:08:58 +03:00
|
|
|
options.Group = uid.String()
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
commitInterval := DefaultCommitInterval
|
2021-09-02 00:20:55 +03:00
|
|
|
if k.opts.Context != nil {
|
|
|
|
if v, ok := k.opts.Context.Value(commitIntervalKey{}).(time.Duration); ok && v > 0 {
|
2023-03-13 21:56:44 +03:00
|
|
|
commitInterval = v
|
2021-09-02 00:20:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 18:28:51 +03:00
|
|
|
sub := &Subscriber{
|
2021-09-15 18:30:07 +03:00
|
|
|
topic: topic,
|
|
|
|
opts: options,
|
|
|
|
handler: handler,
|
|
|
|
kopts: k.opts,
|
2023-03-13 21:56:44 +03:00
|
|
|
consumers: make(map[tp]*consumer),
|
|
|
|
done: make(chan struct{}),
|
2021-09-15 18:30:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
kopts := append(k.kopts,
|
|
|
|
kgo.ConsumerGroup(options.Group),
|
|
|
|
kgo.ConsumeTopics(topic),
|
|
|
|
kgo.ConsumeResetOffset(kgo.NewOffset().AtStart()),
|
2021-08-22 12:10:58 +03:00
|
|
|
kgo.FetchMaxWait(1*time.Second),
|
2023-03-13 21:56:44 +03:00
|
|
|
kgo.AutoCommitInterval(commitInterval),
|
2021-09-15 18:30:07 +03:00
|
|
|
kgo.OnPartitionsAssigned(sub.assigned),
|
|
|
|
kgo.OnPartitionsRevoked(sub.revoked),
|
2023-03-13 21:56:44 +03:00
|
|
|
kgo.OnPartitionsLost(sub.lost),
|
|
|
|
kgo.AutoCommitMarks(),
|
2021-08-09 00:30:36 +03:00
|
|
|
)
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(optionsKey{}).([]kgo.Opt); ok && len(v) > 0 {
|
|
|
|
kopts = append(kopts, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 07:30:17 +03:00
|
|
|
c, htracer, err := k.connect(ctx, kopts...)
|
2021-08-09 00:30:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-13 15:12:26 +03:00
|
|
|
mdreq := kmsg.NewMetadataRequest()
|
|
|
|
mdreq.Topics = []kmsg.MetadataRequestTopic{
|
|
|
|
{Topic: &topic},
|
|
|
|
}
|
|
|
|
|
|
|
|
mdrsp, err := mdreq.RequestWith(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if mdrsp.Topics[0].ErrorCode != 0 {
|
|
|
|
return nil, fmt.Errorf("topic %s not exists or permission error", topic)
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
sub.c = c
|
2024-05-06 07:30:17 +03:00
|
|
|
sub.htracer = htracer
|
2024-04-13 02:24:16 +03:00
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
go sub.poll(ctx)
|
2021-08-09 00:30:36 +03:00
|
|
|
|
|
|
|
k.Lock()
|
|
|
|
k.subs = append(k.subs, sub)
|
|
|
|
k.Unlock()
|
2021-07-10 23:41:21 +03:00
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func (k *Broker) String() string {
|
2021-07-10 23:41:21 +03:00
|
|
|
return "kgo"
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
func NewBroker(opts ...broker.Option) *Broker {
|
2021-08-25 21:51:15 +03:00
|
|
|
options := broker.NewOptions(opts...)
|
2023-03-13 21:56:44 +03:00
|
|
|
|
|
|
|
kaddrs := options.Addrs
|
|
|
|
// shuffle addrs
|
|
|
|
var rng mrand.Rand
|
|
|
|
rng.Shuffle(len(kaddrs), func(i, j int) {
|
|
|
|
kaddrs[i], kaddrs[j] = kaddrs[j], kaddrs[i]
|
|
|
|
})
|
2021-08-25 21:51:15 +03:00
|
|
|
kopts := []kgo.Opt{
|
2023-03-13 21:56:44 +03:00
|
|
|
kgo.DialTimeout(3 * time.Second),
|
2021-09-02 00:20:55 +03:00
|
|
|
kgo.DisableIdempotentWrite(),
|
|
|
|
kgo.ProducerBatchCompression(kgo.NoCompression()),
|
2024-10-12 12:45:53 +03:00
|
|
|
kgo.WithLogger(&mlogger{l: options.Logger.Clone(logger.WithAddCallerSkipCount(2)), ctx: options.Context}),
|
2023-03-13 21:56:44 +03:00
|
|
|
kgo.SeedBrokers(kaddrs...),
|
|
|
|
kgo.RetryBackoffFn(DefaultRetryBackoffFn),
|
|
|
|
kgo.BlockRebalanceOnPoll(),
|
|
|
|
kgo.Balancers(kgo.CooperativeStickyBalancer()),
|
|
|
|
kgo.FetchIsolationLevel(kgo.ReadUncommitted()),
|
2024-10-06 13:53:30 +03:00
|
|
|
kgo.UnknownTopicRetries(0),
|
2021-08-25 21:51:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Context != nil {
|
|
|
|
if v, ok := options.Context.Value(optionsKey{}).([]kgo.Opt); ok && len(v) > 0 {
|
|
|
|
kopts = append(kopts, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:56:44 +03:00
|
|
|
return &Broker{
|
2024-12-05 09:53:40 +03:00
|
|
|
connected: &atomic.Uint32{},
|
|
|
|
opts: options,
|
|
|
|
kopts: kopts,
|
2021-07-10 23:41:21 +03:00
|
|
|
}
|
|
|
|
}
|