Skip to content
Open
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions kq/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type (
// kafka.Writer options
allowAutoTopicCreation bool
balancer kafka.Balancer
batchTimeout time.Duration
batchBytes int64

// executors.ChunkExecutor options
chunkSize int
Expand Down Expand Up @@ -59,6 +61,12 @@ func NewPusher(addrs []string, topic string, opts ...PushOption) *Pusher {
if options.balancer != nil {
producer.Balancer = options.balancer
}
if options.batchTimeout > 0 {
producer.BatchTimeout = options.batchTimeout
}
if options.batchBytes > 0 {
producer.BatchBytes = options.batchBytes
}

pusher := &Pusher{
producer: producer,
Expand Down Expand Up @@ -178,3 +186,17 @@ func WithSyncPush() PushOption {
options.syncPush = true
}
}

// WithBatchTimeout customizes the Pusher with the given batch timeout.
func WithBatchTimeout(timeout time.Duration) PushOption {
return func(options *pushOptions) {
options.batchTimeout = timeout
}
}

// WithBatchBytes customizes the Pusher with the given batch bytes.
func WithBatchBytes(bytes int64) PushOption {
return func(options *pushOptions) {
options.batchBytes = bytes
}
}
38 changes: 38 additions & 0 deletions kq/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ func TestNewPusher(t *testing.T) {
assert.NotNil(t, pusher)
assert.True(t, pusher.producer.(*kafka.Writer).AllowAutoTopicCreation)
})

t.Run("WithBatchTimeout", func(t *testing.T) {
timeout := time.Second * 5
pusher := NewPusher(addrs, topic, WithBatchTimeout(timeout))
assert.NotNil(t, pusher)
assert.Equal(t, timeout, pusher.producer.(*kafka.Writer).BatchTimeout)
})

t.Run("WithBatchBytes", func(t *testing.T) {
batchBytes := int64(1024 * 1024) // 1MB
pusher := NewPusher(addrs, topic, WithBatchBytes(batchBytes))
assert.NotNil(t, pusher)
assert.Equal(t, batchBytes, pusher.producer.(*kafka.Writer).BatchBytes)
})

t.Run("WithMultipleBatchOptions", func(t *testing.T) {
timeout := time.Second * 3
batchBytes := int64(512 * 1024) // 512KB
pusher := NewPusher(addrs, topic, WithBatchTimeout(timeout), WithBatchBytes(batchBytes))
assert.NotNil(t, pusher)
writer := pusher.producer.(*kafka.Writer)
assert.Equal(t, timeout, writer.BatchTimeout)
assert.Equal(t, batchBytes, writer.BatchBytes)
})
}

func TestPusher_Close(t *testing.T) {
Expand Down Expand Up @@ -139,3 +163,17 @@ func TestPusher_PushWithKey_Error(t *testing.T) {
assert.Equal(t, expectedError, err)
mockWriter.AssertExpectations(t)
}

func TestWithBatchTimeout(t *testing.T) {
options := &pushOptions{}
timeout := time.Second * 5
WithBatchTimeout(timeout)(options)
assert.Equal(t, timeout, options.batchTimeout)
}

func TestWithBatchBytes(t *testing.T) {
options := &pushOptions{}
batchBytes := int64(1024 * 1024)
WithBatchBytes(batchBytes)(options)
assert.Equal(t, batchBytes, options.batchBytes)
}