-
Notifications
You must be signed in to change notification settings - Fork 1
/
retry.go
236 lines (200 loc) · 5.21 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Package retry implements a wrapper to retry failing function calls.
package retry
import (
"context"
"errors"
"math"
"time"
)
type backoff interface {
delay(attempt int) time.Duration
}
type internalOptions struct {
Attempts
backoff
budget *Budget
Jitter
Timeout
}
// Option is an option for Do().
//
// The following types implement Option:
//
// • Attempts
//
// • Budget
//
// • ExpBackoff
//
// • Jitter
//
// • Timeout
type Option interface {
apply(*internalOptions)
}
// ExpBackoff sets custom backoff parameters. After the first
// failure, execution pauses for the duration specified by base. After each
// subsequent failure the delay is doubled until max is reached. Execution is
// never paused for longer than the duration max.
//
// Implements the Option interface.
type ExpBackoff struct {
Base time.Duration
Max time.Duration
Factor float64
}
func (b ExpBackoff) apply(opts *internalOptions) {
opts.backoff = b
}
func (b ExpBackoff) delay(attempt int) time.Duration {
f := float64(b.Base) * math.Pow(b.Factor, float64(attempt))
d := time.Duration(f)
if d < b.Base {
return b.Base
} else if d > b.Max {
return b.Max
}
return d
}
// Attempts sets the number of calls made to the callback, i.e. the call is
// attempted at most n times. If all calls fail, the error of the last call is
// returned by Do().
//
// Special case: the zero value retries indefinitely.
//
// Implements the Option interface.
type Attempts int
func (opt Attempts) apply(opts *internalOptions) {
opts.Attempts = opt
}
// Timeout specifies the timeout for each individual attempt. When specified,
// the context passed to the callback is cancelled after this duration. When
// the timeout expires, the callback should return as quickly as possible. The
// retry logic continues without waiting for the callback to return, though, so
// callbacks should be thread-safe.
//
// Implements the Option interface.
type Timeout time.Duration
func (opt Timeout) apply(opts *internalOptions) {
opts.Timeout = opt
}
// Error is an error type that controls retry behavior. If Temporary() returns
// false, Do() returns immediately and does not continue to call the callback
// function.
//
// Error is specifically designed to be a subset of net.Error.
type Error interface {
Temporary() bool
error
}
// permanentError is a persisting error condition.
type permanentError struct {
error
}
func (permanentError) Temporary() bool { return false }
func (e permanentError) Unwrap() error {
return e.error
}
// Abort wraps err so it implements the Error interface and reports a permanent
// condition. This causes Do() to return immediately with the wrapped error.
func Abort(err error) Error {
return permanentError{err}
}
var contextAttemptKey struct{}
func withAttempt(ctx context.Context, attempt int) context.Context {
return context.WithValue(ctx, contextAttemptKey, attempt)
}
// Attempt returns the number of previous attempts. In other words, it returns
// the zero-based index of the request.
//
// Only call this function from within a retried function.
func Attempt(ctx context.Context) int {
i := ctx.Value(contextAttemptKey)
if i == nil {
return 0
}
return i.(int)
}
// Do repeatedly calls cb until it succeeds. After cb fails (returns a non-nil
// error), execution is paused for an exponentially increasing time. Execution
// can be cancelled at any time by cancelling the context.
//
// By default, this function behaves as if the following options were passed:
// Attempts(4),
// ExpBackoff{
// Base: 100 * time.Millisecond,
// Max: 2 * time.Second,
// Factor: 2.0,
// },
// FullJitter,
func Do(ctx context.Context, cb func(context.Context) error, opts ...Option) error {
intOpts := internalOptions{
Attempts: Attempts(4),
backoff: ExpBackoff{
Base: 100 * time.Millisecond,
Max: 2 * time.Second,
Factor: 2.0,
},
Jitter: FullJitter,
}
for _, o := range opts {
o.apply(&intOpts)
}
return do(ctx, cb, intOpts)
}
func do(ctx context.Context, cb func(context.Context) error, opts internalOptions) error {
ch := make(chan error)
var err error
for i := 0; Attempts(i) < opts.Attempts || opts.Attempts == 0; i++ {
ctx := withAttempt(ctx, i)
if !opts.budget.sendOK(i != 0) {
return errors.New("retry budget exhausted")
}
go func(ctx context.Context) {
if opts.Timeout != 0 {
ch <- callWithTimeout(ctx, cb, opts.Timeout)
} else {
ch <- cb(ctx)
}
}(ctx)
select {
case <-ctx.Done():
return ctx.Err()
case err = <-ch:
if err == nil {
return nil
}
if retryErr, ok := err.(Error); ok && !retryErr.Temporary() {
if p, ok := err.(permanentError); ok {
return p.error
}
return err
}
}
delay := opts.delay(i)
delay = opts.jitter(delay)
ticker := time.NewTicker(delay)
select {
case <-ctx.Done():
ticker.Stop()
return ctx.Err()
case <-ticker.C:
ticker.Stop()
}
}
return err
}
func callWithTimeout(ctx context.Context, cb func(context.Context) error, timeout Timeout) error {
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeout))
defer cancel()
ch := make(chan error)
go func(ctx context.Context) {
ch <- cb(ctx)
}(ctx)
select {
case <-ctx.Done():
return ctx.Err()
case err := <-ch:
return err
}
}