Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kq): support custom producer balancer and message key #55

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions kq/pusher.go
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
pushOptions struct {
// kafka.Writer options
allowAutoTopicCreation bool
balancer kafka.Balancer

// executors.ChunkExecutor options
chunkSize int
@@ -50,6 +51,9 @@

// apply kafka.Writer options
producer.AllowAutoTopicCreation = options.allowAutoTopicCreation
if options.balancer != nil {
producer.Balancer = options.balancer
}

pusher := &Pusher{
producer: producer,
@@ -134,6 +138,26 @@
}
}

// SetWriterBalancer set kafka-go custom writer balancer.
func (p *Pusher) SetWriterBalancer(balancer kafka.Balancer) {
if p.producer != nil {
p.producer.Balancer = balancer
}
}

// PushWithKey sends a message to the Kafka topic with custom message key.
func (p *Pusher) PushWithKey(k, v string) error {

Check failure on line 149 in kq/pusher.go

GitHub Actions / Build

method Pusher.PushWithKey already declared at kq/pusher.go:123:18
msg := kafka.Message{
Key: []byte(k), // custom message key
Value: []byte(v),
}
if p.executor != nil {
return p.executor.Add(msg, len(v))
} else {
return p.producer.WriteMessages(context.Background(), msg)
}
}

// WithChunkSize customizes the Pusher with the given chunk size.
func WithChunkSize(chunkSize int) PushOption {
return func(options *pushOptions) {
@@ -155,6 +179,13 @@
}
}

// WithBalancer customizes the Pusher with the given balancer.
func WithBalancer(balancer kafka.Balancer) PushOption {
return func(options *pushOptions) {
options.balancer = balancer
}
}

// WithSyncPush enables the Pusher to push messages synchronously.
func WithSyncPush() PushOption {
return func(options *pushOptions) {