From 1715d14cc8343cf970abc5947a48ce82a627b69e Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Mon, 9 Nov 2026 13:28:54 +0900 Subject: [PATCH] fix(package): [retry] use a bit shift only for power-of-two backoff bases (#427) * fix(package): [retry] use a bit shift only for power-of-two backoff bases * test(package): [retry] cover the multiplier of even backoff bases --- retry/retry.go | 3 ++- retry/retry_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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) + } + } +}