redis/internal/internal.go

30 lines
452 B
Go
Raw Permalink Normal View History

2017-05-25 08:08:44 +03:00
package internal
import (
"time"
2020-06-29 17:26:11 +03:00
2023-01-23 09:48:54 +03:00
"github.com/redis/go-redis/v9/internal/rand"
2017-05-25 08:08:44 +03:00
)
2017-07-09 13:10:07 +03:00
func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {
2017-05-25 08:08:44 +03:00
if retry < 0 {
2020-07-28 15:42:38 +03:00
panic("not reached")
2017-05-25 08:08:44 +03:00
}
2020-07-28 16:14:54 +03:00
if minBackoff == 0 {
return 0
}
2017-05-25 08:08:44 +03:00
2020-07-28 15:42:38 +03:00
d := minBackoff << uint(retry)
2020-10-28 12:12:46 +03:00
if d < minBackoff {
return maxBackoff
}
2020-07-28 15:42:38 +03:00
d = minBackoff + time.Duration(rand.Int63n(int64(d)))
2017-05-25 08:08:44 +03:00
2020-07-28 15:42:38 +03:00
if d > maxBackoff || d < minBackoff {
d = maxBackoff
2017-07-09 13:10:07 +03:00
}
2020-07-28 15:42:38 +03:00
return d
2017-05-25 08:08:44 +03:00
}