From 6e3d4a53c6412f74f61bc3329674bf23b1611715 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 28 Jul 2020 15:42:38 +0300 Subject: [PATCH] Tweak backoff --- internal/internal.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) 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 }