mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
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
This commit is contained in:
1 parent
0eabb67ed4
commit
1715d14cc8
2 files changed
+19
-1
No files matched your search
+2
-1
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user