diff --git a/internal/internal.go b/internal/internal.go index 3fbdc99..6fd344b 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -6,20 +6,17 @@ import ( "golang.org/x/exp/rand" ) -// Retry backoff with jitter sleep to prevent overloaded conditions during intervals -// https://www.awsarchitectureblog.com/2015/03/backoff.html func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration { if retry < 0 { - retry = 0 + panic("not reached") } - backoff := minBackoff << uint(retry) - if backoff > maxBackoff || backoff < minBackoff { - backoff = maxBackoff + d := minBackoff << uint(retry) + d = minBackoff + time.Duration(rand.Int63n(int64(d))) + + if d > maxBackoff || d < minBackoff { + d = maxBackoff } - if backoff == 0 { - return 0 - } - return time.Duration(rand.Int63n(int64(backoff))) + return d }