2017-05-25 08:08:44 +03:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2020-06-29 17:26:11 +03:00
|
|
|
|
|
|
|
"golang.org/x/exp/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)
|
|
|
|
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
|
|
|
}
|