internal.go 452 B

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