-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff.go
119 lines (96 loc) · 2.9 KB
/
backoff.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package boomerang
import (
"math"
"math/rand"
"net/http"
"time"
)
// CheckRetry specifies a policy for handling retries. It is called
// following each request with the response and error values returned by
// the http.Client. If CheckRetry returns false, the Client stops retrying
// and returns the response to the caller. If CheckRetry returns an error,
// that error value is returned in lieu of the error from the request. The
// Client will close any response body when retrying
func init(){
rand.Seed(time.Now().UnixNano())
}
type CheckRetry func(resp *http.Response, err error) (bool, error)
type Backoff interface {
NextInterval(retry int) time.Duration
}
type BackoffFunc func(retry int) time.Duration
func (b BackoffFunc) NextInterval(retry int) time.Duration {
return b(retry)
}
func NewBackoffFunc(f BackoffFunc) Backoff {
return f
}
type exponentialBackoff struct {
factor float64
minTimeout time.Duration
maxTimeout time.Duration
}
// NewExponentialBackoff returns an instance of ExponentialBackoff
func NewExponentialBackoff(minTimeout, maxTimeout time.Duration, exponentFactor float64) Backoff {
return &exponentialBackoff{
factor: exponentFactor,
minTimeout: minTimeout,
maxTimeout: maxTimeout,
}
}
// Next returns next time for retrying operation with exponential strategy
func (e *exponentialBackoff) NextInterval(retryCount int) time.Duration {
if retryCount <= 0 {
return 0 * time.Millisecond
}
efac := math.Pow(e.factor, float64(retryCount)) * float64(e.minTimeout)
sleep := math.Min(efac, float64(e.maxTimeout))
return time.Duration(sleep)
}
type jitterBackoff struct {
factor float64
minTimeout time.Duration
maxTimeout time.Duration
}
// NewExponentialBackoff returns an instance of ExponentialBackoff
func NewJitterBackoff(minTimeout, maxTimeout time.Duration, exponentFactor float64) Backoff {
return &jitterBackoff{
factor: exponentFactor,
minTimeout: minTimeout,
maxTimeout: maxTimeout,
}
}
// Next returns next time for retrying operation with exponential strategy
func (e *jitterBackoff) NextInterval(retryCount int) time.Duration {
if retryCount <= 0 {
return 0 * time.Millisecond
}
//calculate this duration
minf := float64(e.minTimeout)
durf := minf * math.Pow(e.factor, float64(retryCount))
durf = rand.Float64()*(durf-minf) + minf
dur := time.Duration(durf)
//keep within bounds
if dur < e.minTimeout {
return e.minTimeout
} else if dur > e.maxTimeout {
return e.maxTimeout
}
return dur
}
type constantBackoff struct {
timeout time.Duration
}
// NewConstanctBackoff returns an instance of ConstantBackoff
func NewConstantBackoff(timeout time.Duration) Backoff {
return &constantBackoff{
timeout: timeout,
}
}
// Next returns next time for retrying operation with exponential strategy
func (cb *constantBackoff) NextInterval(retryCount int) time.Duration {
if retryCount <= 0 {
return 0 * time.Millisecond
}
return cb.timeout
}