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.

30 lines
452 B

  1. package internal
  2. import (
  3. "time"
  4. "github.com/go-redis/redis/v8/internal/rand"
  5. )
  6. func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {
  7. if retry < 0 {
  8. panic("not reached")
  9. }
  10. if minBackoff == 0 {
  11. return 0
  12. }
  13. d := minBackoff << uint(retry)
  14. if d < minBackoff {
  15. return maxBackoff
  16. }
  17. d = minBackoff + time.Duration(rand.Int63n(int64(d)))
  18. if d > maxBackoff || d < minBackoff {
  19. d = maxBackoff
  20. }
  21. return d
  22. }