/
retry.go
36 lines (30 loc) · 1.03 KB
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package util
import (
"time"
"github.com/cenkalti/backoff"
)
// Retry retries with exponential backoff the given function
func Retry(maxElapsedTime time.Duration, f func() error) error {
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = maxElapsedTime
bo.Reset()
return backoff.Retry(f, bo)
}
// RetryWithInitialDelay retires with exponential backoff and initial delay the given function
func RetryWithInitialDelay(initialDelay, maxElapsedTime time.Duration, f func() error) error {
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = initialDelay
bo.MaxElapsedTime = maxElapsedTime
bo.Multiplier = 2.0
bo.Reset()
return backoff.Retry(f, bo)
}
// RetryWithInitialDelaySlower retries with exponential backoff, an initial delay and with a slower rate
func RetryWithInitialDelaySlower(initialDelay, maxElapsedTime time.Duration, f func() error) error {
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = initialDelay
bo.MaxElapsedTime = maxElapsedTime
bo.Multiplier = 2.0
bo.Reset()
return backoff.Retry(f, bo)
}