Skip to content

Commit

Permalink
stop overriding max backoff when none is set (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanhanner authored Jul 28, 2020
1 parent 3788375 commit 45113e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func WithMaxAttempts(maxAttempts int) Option {
// WithInitialBackoff sets initial backoff.
//
// If initial backoff option is not used, then default value of 50 milliseconds is used.
// If initial backoff is larger than max backoff, it takes precedence.
// If initial backoff is larger than max backoff and the max backoff is nonzero, the initial backoff will be
// used as the max.
func WithInitialBackoff(initialBackoff time.Duration) Option {
return func(o *options) {
o.initialBackoff = initialBackoff
Expand Down Expand Up @@ -181,8 +182,10 @@ func Start(ctx context.Context, opts ...Option) Retrier {
for _, option := range opts {
option(&r.options)
}
// If initial backoff is larger than max backoff, it takes precedence.
r.options.maxBackoff = max(r.options.maxBackoff, r.options.initialBackoff)
// If initial backoff is larger than max backoff and the max backoff is set, initial takes precedence.
if r.options.maxBackoff != 0 {
r.options.maxBackoff = max(r.options.maxBackoff, r.options.initialBackoff)
}
r.Reset()
return r
}
Expand Down
21 changes: 21 additions & 0 deletions retry/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ func TestRetrier_Next_WithInitialBackoffLargerThanMax(t *testing.T) {
}
}

func TestRetrier_Next_WithInitialBackoffLargerThanMaxAndMaxNotSet(t *testing.T) {
const initialBackoff = time.Second * 5
const maxBackoff = 0
const multiplier = 2

options := []Option{
WithInitialBackoff(initialBackoff),
WithMaxBackoff(maxBackoff),
WithMultiplier(multiplier),
WithMaxAttempts(11),
WithRandomizationFactor(0),
}

r := Start(context.Background(), options...).(*retrier)
r.currentAttempt = 1
d := r.retryIn()
if d != initialBackoff*multiplier {
t.Fatalf("expected second attempt to increase backoff: %s vs %s", d, initialBackoff*multiplier)
}
}

func TestRetrier_Next_WithMaxAttempts(t *testing.T) {
const maxAttempts = 2

Expand Down

0 comments on commit 45113e6

Please sign in to comment.