2020-04-08 11:53:02 +03:00
|
|
|
package segmentio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-08 14:11:14 +03:00
|
|
|
"time"
|
2020-04-08 11:53:02 +03:00
|
|
|
|
|
|
|
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/client"
|
2020-04-08 11:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-07-18 13:28:13 +03:00
|
|
|
DefaultReaderConfig = kafka.ReaderConfig{}
|
|
|
|
DefaultWriterConfig = kafka.WriterConfig{}
|
|
|
|
DefaultStatsInterval = time.Second * 10
|
|
|
|
DefaultCommitInterval = time.Second * 2
|
|
|
|
DefaultCommitQueueSize = 2000
|
2020-04-08 11:53:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type readerConfigKey struct{}
|
|
|
|
type writerConfigKey struct{}
|
|
|
|
|
|
|
|
func ReaderConfig(c kafka.ReaderConfig) broker.Option {
|
2021-01-19 15:53:47 +03:00
|
|
|
return broker.SetOption(readerConfigKey{}, c)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriterConfig(c kafka.WriterConfig) broker.Option {
|
2021-01-19 15:53:47 +03:00
|
|
|
return broker.SetOption(writerConfigKey{}, c)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type subscribeContextKey struct{}
|
|
|
|
|
|
|
|
// SubscribeContext set the context for broker.SubscribeOption
|
|
|
|
func SubscribeContext(ctx context.Context) broker.SubscribeOption {
|
2021-01-19 15:53:47 +03:00
|
|
|
return broker.SetSubscribeOption(subscribeContextKey{}, ctx)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type subscribeReaderConfigKey struct{}
|
|
|
|
|
|
|
|
func SubscribeReaderConfig(c kafka.ReaderConfig) broker.SubscribeOption {
|
2021-01-19 15:53:47 +03:00
|
|
|
return broker.SetSubscribeOption(subscribeReaderConfigKey{}, c)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type subscribeWriterConfigKey struct{}
|
|
|
|
|
|
|
|
func SubscribeWriterConfig(c kafka.WriterConfig) broker.SubscribeOption {
|
2021-01-19 15:53:47 +03:00
|
|
|
return broker.SetSubscribeOption(subscribeWriterConfigKey{}, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type publishKey struct{}
|
|
|
|
|
|
|
|
func PublishKey(key []byte) broker.PublishOption {
|
|
|
|
return broker.SetPublishOption(publishKey{}, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClientPublishKey(key []byte) client.PublishOption {
|
|
|
|
return client.SetPublishOption(publishKey{}, key)
|
2020-04-08 11:53:02 +03:00
|
|
|
}
|
2021-07-08 14:11:14 +03:00
|
|
|
|
|
|
|
type statsIntervalKey struct{}
|
|
|
|
|
|
|
|
func StatsInterval(td time.Duration) broker.Option {
|
|
|
|
return broker.SetOption(statsIntervalKey{}, td)
|
|
|
|
}
|
|
|
|
|
|
|
|
type writerCompletionFunc struct{}
|
|
|
|
|
|
|
|
func WriterCompletionFunc(fn func([]kafka.Message, error)) broker.Option {
|
|
|
|
return broker.SetOption(writerCompletionFunc{}, fn)
|
|
|
|
}
|
2021-07-18 13:28:13 +03:00
|
|
|
|
|
|
|
type clientIDKey struct{}
|
|
|
|
|
|
|
|
func ClientID(id string) broker.Option {
|
|
|
|
return broker.SetOption(clientIDKey{}, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
type commitIntervalKey struct{}
|
|
|
|
|
|
|
|
func CommitInterval(td time.Duration) broker.Option {
|
|
|
|
return broker.SetOption(commitIntervalKey{}, td)
|
|
|
|
}
|