You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
513 B

  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. }