2019-11-21 16:19:22 +00:00
|
|
|
package tt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cenkalti/backoff/v3"
|
|
|
|
)
|
|
|
|
|
2021-02-17 23:14:48 +00:00
|
|
|
type BackOffOption func(*backoff.ExponentialBackOff)
|
|
|
|
|
|
|
|
func RetryWithBackOff(o func() error, options ...BackOffOption) error {
|
2019-11-21 16:19:22 +00:00
|
|
|
b := backoff.ExponentialBackOff{
|
|
|
|
InitialInterval: time.Millisecond * 100,
|
|
|
|
RandomizationFactor: 0.1,
|
|
|
|
Multiplier: 1,
|
|
|
|
MaxInterval: time.Second,
|
|
|
|
MaxElapsedTime: time.Second * 10,
|
|
|
|
Clock: backoff.SystemClock,
|
|
|
|
}
|
2021-02-17 23:14:48 +00:00
|
|
|
for _, option := range options {
|
|
|
|
option(&b)
|
|
|
|
}
|
2019-11-21 16:19:22 +00:00
|
|
|
b.Reset()
|
|
|
|
return backoff.Retry(o, &b)
|
|
|
|
}
|