2021-07-10 23:41:21 +03:00
|
|
|
package kgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
kgo "github.com/twmb/franz-go/pkg/kgo"
|
2021-10-03 18:30:56 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
2021-12-20 08:47:34 +03:00
|
|
|
"go.unistack.org/micro/v3/server"
|
2021-07-10 23:41:21 +03:00
|
|
|
)
|
|
|
|
|
2021-08-25 21:51:15 +03:00
|
|
|
// DefaultCommitInterval specifies how fast send commit offsets to kafka
|
|
|
|
var DefaultCommitInterval = 5 * time.Second
|
2021-08-24 13:20:31 +03:00
|
|
|
|
2021-07-10 23:41:21 +03:00
|
|
|
type subscribeContextKey struct{}
|
|
|
|
|
|
|
|
// SubscribeContext set the context for broker.SubscribeOption
|
|
|
|
func SubscribeContext(ctx context.Context) broker.SubscribeOption {
|
|
|
|
return broker.SetSubscribeOption(subscribeContextKey{}, ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type publishKey struct{}
|
|
|
|
|
2021-08-09 16:49:04 +03:00
|
|
|
// PublishKey set the kafka message key (broker option)
|
2021-07-10 23:41:21 +03:00
|
|
|
func PublishKey(key []byte) broker.PublishOption {
|
|
|
|
return broker.SetPublishOption(publishKey{}, key)
|
|
|
|
}
|
|
|
|
|
2021-08-09 16:49:04 +03:00
|
|
|
// ClientPublishKey set the kafka message key (client option)
|
2021-07-10 23:41:21 +03:00
|
|
|
func ClientPublishKey(key []byte) client.PublishOption {
|
|
|
|
return client.SetPublishOption(publishKey{}, key)
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:30:36 +03:00
|
|
|
type optionsKey struct{}
|
|
|
|
|
2021-08-09 16:49:04 +03:00
|
|
|
// Options pass additional options to broker
|
2021-08-09 00:30:36 +03:00
|
|
|
func Options(opts ...kgo.Opt) broker.Option {
|
2021-08-25 21:51:15 +03:00
|
|
|
return func(o *broker.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
options, ok := o.Context.Value(optionsKey{}).([]kgo.Opt)
|
|
|
|
if !ok {
|
|
|
|
options = make([]kgo.Opt, 0, len(opts))
|
|
|
|
}
|
|
|
|
options = append(options, opts...)
|
|
|
|
o.Context = context.WithValue(o.Context, optionsKey{}, options)
|
|
|
|
}
|
2021-08-09 00:30:36 +03:00
|
|
|
}
|
2021-08-24 13:20:31 +03:00
|
|
|
|
2021-12-20 08:47:34 +03:00
|
|
|
// SubscribeOptions pass additional options to broker
|
|
|
|
func SubscribeOptions(opts ...kgo.Opt) broker.SubscribeOption {
|
|
|
|
return func(o *broker.SubscribeOptions) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
options, ok := o.Context.Value(optionsKey{}).([]kgo.Opt)
|
|
|
|
if !ok {
|
|
|
|
options = make([]kgo.Opt, 0, len(opts))
|
|
|
|
}
|
|
|
|
options = append(options, opts...)
|
|
|
|
o.Context = context.WithValue(o.Context, optionsKey{}, options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscriberOptions pass additional options to broker
|
|
|
|
func SubscriberOptions(opts ...kgo.Opt) server.SubscriberOption {
|
|
|
|
return func(o *server.SubscriberOptions) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
options, ok := o.Context.Value(optionsKey{}).([]kgo.Opt)
|
|
|
|
if !ok {
|
|
|
|
options = make([]kgo.Opt, 0, len(opts))
|
|
|
|
}
|
|
|
|
options = append(options, opts...)
|
|
|
|
o.Context = context.WithValue(o.Context, optionsKey{}, options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:20:31 +03:00
|
|
|
type commitIntervalKey struct{}
|
|
|
|
|
|
|
|
// CommitInterval specifies interval to send commits
|
|
|
|
func CommitInterval(td time.Duration) broker.Option {
|
|
|
|
return broker.SetOption(commitIntervalKey{}, td)
|
|
|
|
}
|
2021-09-15 18:30:07 +03:00
|
|
|
|
|
|
|
var DefaultSubscribeMaxInflight = 1000
|
|
|
|
|
|
|
|
type subscribeMaxInflightKey struct{}
|
|
|
|
|
2021-10-27 17:47:25 +03:00
|
|
|
// SubscribeMaxInFlight max queued messages
|
2021-09-15 18:30:07 +03:00
|
|
|
func SubscribeMaxInFlight(n int) broker.SubscribeOption {
|
|
|
|
return broker.SetSubscribeOption(subscribeMaxInflightKey{}, n)
|
|
|
|
}
|