Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | package internal |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/go-redis/redis/v8/internal/rand" |
| 7 | ) |
| 8 | |
| 9 | func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration { |
| 10 | if retry < 0 { |
| 11 | panic("not reached") |
| 12 | } |
| 13 | if minBackoff == 0 { |
| 14 | return 0 |
| 15 | } |
| 16 | |
| 17 | d := minBackoff << uint(retry) |
| 18 | if d < minBackoff { |
| 19 | return maxBackoff |
| 20 | } |
| 21 | |
| 22 | d = minBackoff + time.Duration(rand.Int63n(int64(d))) |
| 23 | |
| 24 | if d > maxBackoff || d < minBackoff { |
| 25 | d = maxBackoff |
| 26 | } |
| 27 | |
| 28 | return d |
| 29 | } |