2017-05-25 08:08:44 +03:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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)))
|
|
|
|
}
|