blob: 4a59c599be77d76687e7a7d591dbfda40ef7fb8a [file] [log] [blame]
Joey Armstrong903c69d2024-02-01 19:46:39 -05001package internal
2
3import (
4 "time"
5
6 "github.com/go-redis/redis/v8/internal/rand"
7)
8
9func 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}