-
Notifications
You must be signed in to change notification settings - Fork 6
/
retry.go
56 lines (44 loc) · 1.5 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package retry
import (
"math/rand"
"net/http"
"time"
)
var (
defaultSleep = 500 * time.Millisecond
)
// Func is the function to be executed and eventually retried.
type Func func() error
// HTTPFunc is the function to be executed and eventually retried.
// The only difference from Func is that it expects an *http.Response on the first returning argument.
type HTTPFunc func() (*http.Response, error)
// Do runs the passed function until the number of retries is reached.
// Whenever Func returns err it will sleep and Func will be executed again in a recursive fashion.
// The sleep value is slightly modified on every retry (exponential backoff) to prevent the thundering herd problem (https://en.wikipedia.org/wiki/Thundering_herd_problem).
// If no value is given to sleep it will defaults to 500ms.
func Do(fn Func, retries int, sleep time.Duration) error {
if sleep == 0 {
sleep = defaultSleep
}
if err := fn(); err != nil {
retries--
if retries <= 0 {
return err
}
// preventing thundering herd problem (https://en.wikipedia.org/wiki/Thundering_herd_problem)
sleep += (time.Duration(rand.Int63n(int64(sleep)))) / 2
time.Sleep(sleep)
return Do(fn, retries, 2*sleep)
}
return nil
}
// DoHTTP wraps Func and returns *http.Response and error as returning arguments.
func DoHTTP(fn HTTPFunc, retries int, sleep time.Duration) (*http.Response, error) {
var res *http.Response
err := Do(func() error {
var err error
res, err = fn()
return err
}, retries, sleep)
return res, err
}