diff --git a/internal/internal.go b/internal/internal.go index e783d139a..e238c4821 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -16,13 +16,14 @@ func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration d := minBackoff << uint(retry) if d < minBackoff { - return maxBackoff + d = minBackoff } - d = minBackoff + time.Duration(rand.Int63n(int64(d))) + jitter := time.Duration(rand.Int63n(int64(d))) - if d > maxBackoff || d < minBackoff { - d = maxBackoff + d = d + jitter + if d > maxBackoff { + return maxBackoff } return d diff --git a/internal/internal_test.go b/internal/internal_test.go index 00bfd6ee2..95d4a654a 100644 --- a/internal/internal_test.go +++ b/internal/internal_test.go @@ -14,5 +14,12 @@ func TestRetryBackoff(t *testing.T) { backoff := RetryBackoff(i, time.Millisecond, 512*time.Millisecond) Expect(backoff >= 0).To(BeTrue()) Expect(backoff <= 512*time.Millisecond).To(BeTrue()) + + expectedExponential := time.Millisecond << uint(i) + if expectedExponential <= 512*time.Millisecond { + Expect(backoff >= expectedExponential).To(BeTrue(), + "Backoff %v should be at least exponential %v for retry %d", + backoff, expectedExponential, i) + } } }