diff --git a/retry/retry.go b/retry/retry.go index c5533c1..d181c9f 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -81,7 +81,8 @@ func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, panic("programming error: retry maxJitter should not be lower to 0") } - if base%2 == 0 { + // only a power of two can be replaced by a bit shift without changing the multiplier + if base != 0 && base&(base-1) == 0 { return func(rc *RetryConfig) { rc.backoffStrategy = &shiftExponentialWithJitter{ interval: interval, diff --git a/retry/retry_test.go b/retry/retry_test.go index ac2aac7..ce18da4 100644 --- a/retry/retry_test.go +++ b/retry/retry_test.go @@ -258,3 +258,20 @@ func TestCancelRetry(t *testing.T) { assert.IsNotNil(err) assert.Equal(4, number) } + +func TestRetryExponentialBackoffMultiplier(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestRetryExponentialBackoffMultiplier") + + for _, base := range []uint64{2, 3, 4, 6, 8, 10} { + rc := &RetryConfig{} + RetryWithExponentialWithJitterBackoff(time.Millisecond, base, 0)(rc) + + want := time.Millisecond + for i := 0; i < 3; i++ { + assert.Equal(want, rc.backoffStrategy.CalculateInterval()) + want *= time.Duration(base) + } + } +}