forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/internal/internal.go

42 lines
464 B
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package internal
import (
"time"
"github.com/redis/go-redis/v9/internal/rand"
)
func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {
2024-02-18 10:42:21 +00:00
if retry < 0 {
2024-02-18 10:42:21 +00:00
panic("not reached")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if minBackoff == 0 {
2024-02-18 10:42:21 +00:00
return 0
2024-02-18 10:42:21 +00:00
}
d := minBackoff << uint(retry)
2024-02-18 10:42:21 +00:00
if d < minBackoff {
2024-02-18 10:42:21 +00:00
return maxBackoff
2024-02-18 10:42:21 +00:00
}
d = minBackoff + time.Duration(rand.Int63n(int64(d)))
if d > maxBackoff || d < minBackoff {
2024-02-18 10:42:21 +00:00
d = maxBackoff
2024-02-18 10:42:21 +00:00
}
return d
2024-02-18 10:42:21 +00:00
}