Skip to content

Commit

Permalink
Introduce OptionFunc to expose Producer.RequiredAcks (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelos Valsamis authored and Sotirios Mantziaris committed Sep 6, 2019
1 parent c4a3ca0 commit b0c5863
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions trace/kafka/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import (
"github.com/beatlabs/patron/log"
)

// RequiredAcks is used in Produce Requests to tell the broker how many replica acknowledgements
// it must see before responding.
type RequiredAcks int16

const (
// NoResponse doesn't send any response, the TCP ACK is all you get.
NoResponse RequiredAcks = 0
// WaitForLocal waits for only the local commit to succeed before responding.
WaitForLocal RequiredAcks = 1
// WaitForAll waits for all in-sync replicas to commit before responding.
WaitForAll RequiredAcks = -1
)

// OptionFunc definition for configuring the async producer in a functional way.
type OptionFunc func(*AsyncProducer) error

Expand Down Expand Up @@ -38,3 +51,12 @@ func Timeouts(dial time.Duration) OptionFunc {
return nil
}
}

// RequiredAcksPolicy option for adjusting how many replica acknowledgements
// broker must see before responding.
func RequiredAcksPolicy(ack RequiredAcks) OptionFunc {
return func(ap *AsyncProducer) error {
ap.cfg.Producer.RequiredAcks = sarama.RequiredAcks(ack)
return nil
}
}
26 changes: 26 additions & 0 deletions trace/kafka/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,29 @@ func TestTimeouts(t *testing.T) {
})
}
}

func TestRequiredAcksPolicy(t *testing.T) {
type args struct {
requiredAcks RequiredAcks
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "success", args: args{requiredAcks: NoResponse}, wantErr: false},
{name: "success", args: args{requiredAcks: WaitForAll}, wantErr: false},
{name: "success", args: args{requiredAcks: WaitForLocal}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ap := AsyncProducer{cfg: sarama.NewConfig()}
err := RequiredAcksPolicy(tt.args.requiredAcks)(&ap)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit b0c5863

Please sign in to comment.