-
Notifications
You must be signed in to change notification settings - Fork 27
/
execution.go
299 lines (244 loc) · 7.48 KB
/
execution.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package failsafe
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/failsafe-go/failsafe-go/common"
)
// ExecutionStats contains execution stats.
type ExecutionStats interface {
// Attempts returns the number of execution attempts so far, including attempts that are currently in progress and
// attempts that were blocked before being executed, such as by a CircuitBreaker or RateLimiter. These can include an initial
// execution along with retries and hedges.
Attempts() int
// Executions returns the number of completed executions. Executions that are blocked, such as when a CircuitBreaker is
// open, are not counted.
Executions() int
// Retries returns the number of retries so far, including retries that are currently in progress.
Retries() int
// Hedges returns the number of hedges that have been executed so far, including hedges that are currently in progress.
Hedges() int
// StartTime returns the time that the initial execution attempt started at.
StartTime() time.Time
// ElapsedTime returns the elapsed time since initial execution attempt began.
ElapsedTime() time.Duration
}
// ExecutionAttempt contains information for an execution attempt.
type ExecutionAttempt[R any] interface {
ExecutionStats
// LastResult returns the result, if any, from the last execution attempt.
LastResult() R
// LastError returns the error, if any, from the last execution attempt.
LastError() error
// IsFirstAttempt returns true when Attempts is 1, meaning this is the first execution attempt.
IsFirstAttempt() bool
// IsRetry returns true when Attempts is > 1, meaning the execution is being retried.
IsRetry() bool
// IsHedge returns true when the execution is part of a hedged attempt.
IsHedge() bool
// AttemptStartTime returns the time that the most recent execution attempt started at.
AttemptStartTime() time.Time
// ElapsedAttemptTime returns the elapsed time since the last execution attempt began.
ElapsedAttemptTime() time.Duration
}
// Execution contains information about an execution.
type Execution[R any] interface {
ExecutionAttempt[R]
// Context returns the context configured for the execution, else context.Background if none was configured. For
// executions involving a timeout or hedge, each attempt will get a separate child context.
Context() context.Context
// IsCanceled returns whether the execution has been canceled by an external Context or a timeout.Timeout.
IsCanceled() bool
// Canceled returns a channel that is closed when the execution is canceled, either by an external Context or a
// timeout.Timeout.
Canceled() <-chan struct{}
}
// A closed channel that can be used as a canceled channel where the canceled channel would have been closed before it
// was accessed.
var closedChan chan any
func init() {
closedChan = make(chan any, 1)
close(closedChan)
}
type execution[R any] struct {
// Shared state across instances
mtx *sync.Mutex
startTime time.Time
attempts *atomic.Uint32
retries *atomic.Uint32
hedges *atomic.Uint32
executions *atomic.Uint32
// Partly shared cancellation state
ctx context.Context
cancelFunc context.CancelFunc
canceledResult **common.PolicyResult[R]
// Per execution state
attemptStartTime time.Time
isHedge bool
lastResult R // The last error that occurred, else the zero value for R.
lastError error // The last error that occurred, else nil.
}
var _ Execution[any] = &execution[any]{}
var _ ExecutionStats = &execution[any]{}
func (e *execution[R]) Attempts() int {
return int(e.attempts.Load())
}
func (e *execution[R]) Executions() int {
return int(e.executions.Load())
}
func (e *execution[R]) Retries() int {
return int(e.retries.Load())
}
func (e *execution[R]) Hedges() int {
return int(e.hedges.Load())
}
func (e *execution[R]) StartTime() time.Time {
return e.startTime
}
func (e *execution[R]) IsFirstAttempt() bool {
return e.attempts.Load() == 1
}
func (e *execution[R]) IsRetry() bool {
return e.attempts.Load() > 1
}
func (e *execution[R]) IsHedge() bool {
return e.isHedge
}
func (e *execution[R]) ElapsedTime() time.Duration {
return time.Since(e.startTime)
}
func (e *execution[R]) LastResult() R {
return e.lastResult
}
func (e *execution[R]) LastError() error {
if e.lastError == nil && e.ctx.Err() != nil {
return e.ctx.Err()
}
return e.lastError
}
func (e *execution[R]) Context() context.Context {
return e.ctx
}
func (e *execution[R]) AttemptStartTime() time.Time {
return e.attemptStartTime
}
func (e *execution[_]) ElapsedAttemptTime() time.Duration {
return time.Since(e.attemptStartTime)
}
func (e *execution[_]) IsCanceled() bool {
return e.ctx.Err() != nil
}
func (e *execution[_]) Canceled() <-chan struct{} {
return e.ctx.Done()
}
func (e *execution[R]) RecordResult(result *common.PolicyResult[R]) *common.PolicyResult[R] {
// Lock to guard against a race with a Timeout canceling the execution
e.mtx.Lock()
defer e.mtx.Unlock()
if canceled, cancelResult := e.isCanceledWithResult(); canceled {
return cancelResult
}
if result != nil {
e.lastResult = result.Result
e.lastError = result.Error
}
return nil
}
func (e *execution[R]) InitializeRetry() *common.PolicyResult[R] {
// Lock to guard against a race with a Timeout canceling the execution
e.mtx.Lock()
defer e.mtx.Unlock()
if canceled, cancelResult := e.isCanceledWithResult(); canceled {
return cancelResult
}
if e.attempts.Add(1) > 1 {
e.retries.Add(1)
}
e.attemptStartTime = time.Now()
*e.canceledResult = nil
return nil
}
func (e *execution[R]) Cancel(result *common.PolicyResult[R]) *common.PolicyResult[R] {
e.mtx.Lock()
defer e.mtx.Unlock()
if canceled, cancelResult := e.isCanceledWithResult(); canceled {
return cancelResult
}
*e.canceledResult = result
if result != nil {
e.lastResult = result.Result
e.lastError = result.Error
}
if e.cancelFunc != nil {
e.cancelFunc()
}
return result
}
func (e *execution[R]) IsCanceledWithResult() (bool, *common.PolicyResult[R]) {
e.mtx.Lock()
defer e.mtx.Unlock()
return e.isCanceledWithResult()
}
// isCanceledWithResult must be locked externally
func (e *execution[R]) isCanceledWithResult() (bool, *common.PolicyResult[R]) {
if e.ctx.Err() != nil {
if *e.canceledResult == nil {
return true, &common.PolicyResult[R]{
Error: e.ctx.Err(),
Done: true,
}
}
return true, *e.canceledResult
}
return false, nil
}
func (e *execution[R]) CopyWithResult(result *common.PolicyResult[R]) Execution[R] {
c := e.copy()
if result != nil {
c.lastResult = result.Result
c.lastError = result.Error
}
return c
}
func (e *execution[R]) CopyForCancellable() Execution[R] {
c := e.copy()
c.ctx, c.cancelFunc = context.WithCancel(c.ctx)
return c
}
func (e *execution[R]) CopyForHedge() Execution[R] {
c := e.copy()
c.isHedge = true
c.attempts.Add(1)
c.hedges.Add(1)
return c
}
func (e *execution[R]) copy() *execution[R] {
e.mtx.Lock()
c := *e
e.mtx.Unlock()
return &c
}
func (e *execution[R]) record() {
e.executions.Add(1)
}
func newExecution[R any](ctx context.Context) *execution[R] {
attempts := atomic.Uint32{}
retries := atomic.Uint32{}
hedges := atomic.Uint32{}
executions := atomic.Uint32{}
attempts.Add(1)
var canceledResult *common.PolicyResult[R]
now := time.Now()
return &execution[R]{
ctx: ctx,
mtx: &sync.Mutex{},
attempts: &attempts,
retries: &retries,
hedges: &hedges,
executions: &executions,
canceledResult: &canceledResult,
attemptStartTime: now,
startTime: now,
}
}