internal.go 513 B

12345678910111213141516171819202122232425
  1. package internal
  2. import (
  3. "math/rand"
  4. "time"
  5. )
  6. // Retry backoff with jitter sleep to prevent overloaded conditions during intervals
  7. // https://www.awsarchitectureblog.com/2015/03/backoff.html
  8. func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {
  9. if retry < 0 {
  10. retry = 0
  11. }
  12. backoff := minBackoff << uint(retry)
  13. if backoff > maxBackoff || backoff < minBackoff {
  14. backoff = maxBackoff
  15. }
  16. if backoff == 0 {
  17. return 0
  18. }
  19. return time.Duration(rand.Int63n(int64(backoff)))
  20. }