2017-05-25 08:08:44 +03:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2020-06-29 17:26:11 +03:00
|
|
|
|
|
|
|
"golang.org/x/exp/rand"
|
2017-05-25 08:08:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Retry backoff with jitter sleep to prevent overloaded conditions during intervals
|
|
|
|
// https://www.awsarchitectureblog.com/2015/03/backoff.html
|
2017-07-09 13:10:07 +03:00
|
|
|
func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {
|
2017-05-25 08:08:44 +03:00
|
|
|
if retry < 0 {
|
|
|
|
retry = 0
|
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
backoff := minBackoff << uint(retry)
|
|
|
|
if backoff > maxBackoff || backoff < minBackoff {
|
|
|
|
backoff = maxBackoff
|
2017-05-25 08:08:44 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 13:10:07 +03:00
|
|
|
if backoff == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2017-05-25 08:08:44 +03:00
|
|
|
return time.Duration(rand.Int63n(int64(backoff)))
|
|
|
|
}
|