-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.go
52 lines (44 loc) · 955 Bytes
/
funcs.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Package funcs provides meta-functions for method execution.
package funcs
import (
"time"
timeUtils "github.com/bertjohnson/util/time"
)
// RetryN calls method up to retries times with exponential backoff until no error is returned.
func RetryN(method func() (bool, error), retries int) error {
// Ensure there is a retry.
if retries < 1 {
retries = 1
}
var isFinal bool
var err error
for i := 0; i < retries; i++ {
isFinal, err = method()
if isFinal {
return err
}
if err == nil {
return nil
}
timeUtils.SleepIncremental(i)
}
return err
}
// RetryUnlimited calls method as many times as required with constant backoff until no error is returned.
func RetryUnlimited(method func() (bool, error)) error {
var isFinal bool
var err error
attempts := 0
delay := time.Second
for {
isFinal, err = method()
if isFinal {
return err
}
if err == nil {
return nil
}
attempts++
time.Sleep(delay)
}
}