-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathretry.go
More file actions
774 lines (667 loc) · 20 KB
/
Copy pathretry.go
File metadata and controls
774 lines (667 loc) · 20 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Copyright (c) 2026 Onur Cinar.
// The source code is provided under MIT License.
// https://github.com/cinar/resile
package resile
import (
"context"
"errors"
"runtime/debug"
"sync"
"time"
"github.com/cinar/resile/chaos"
"github.com/cinar/resile/circuit"
)
// Retryer defines the interface for executing actions with resilience.
type Retryer interface {
// Do executes a function that returns a value and an error.
Do(ctx context.Context, action func(context.Context) (any, error)) (any, error)
// DoHedged executes a function using speculative retries (hedging).
DoHedged(ctx context.Context, action func(context.Context) (any, error)) (any, error)
// DoErr executes a function that returns only an error.
DoErr(ctx context.Context, action func(context.Context) error) error
// DoErrHedged executes a function using speculative retries (hedging).
DoErrHedged(ctx context.Context, action func(context.Context) error) error
}
// PanicError represents a recovered panic during execution.
type PanicError struct {
Value any
StackTrace string
}
// Error implements the error interface.
func (p *PanicError) Error() string {
return "panic: " + p.StackTrace
}
// Config represents the configuration for the retry execution.
type Config struct {
Name string
MaxAttempts uint
BaseDelay time.Duration
MaxDelay time.Duration
HedgingDelay time.Duration
Backoff Backoff
Policy *retryPolicy
Instrumenter Instrumenter
CircuitBreaker *circuit.Breaker
Fallback any
AdaptiveBucket *AdaptiveBucket
RecoverPanics bool
Bulkhead *Bulkhead
PriorityBulkhead *PriorityBulkhead
Timeout time.Duration
RateLimiter *RateLimiter
AdaptiveLimiter *AdaptiveLimiter
Chaos *chaos.Injector
MinDeadlineThreshold time.Duration
pipeline []middleware
composedPipeline doAction
mu sync.RWMutex
}
// terminalAction is the base action that retrieves the action from RetryState.
func terminalAction(ctx context.Context, state RetryState) error {
if state.simpleAction != nil {
return state.simpleAction(ctx)
}
if state.action == nil {
return nil
}
return state.action(ctx, state)
}
func (c *Config) adaptiveLimiterMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if c.AdaptiveLimiter == nil {
return next(ctx, state)
}
return c.AdaptiveLimiter.Execute(ctx, func() error {
return next(ctx, state)
})
}
}
}
// Do executes an action with retry logic using the provided options.
// This generic function handles functions returning (T, error).
func Do[T any](ctx context.Context, action func(context.Context) (T, error), opts ...Option) (T, error) {
return DoState(ctx, func(innerCtx context.Context, _ RetryState) (T, error) {
return action(innerCtx)
}, opts...)
}
// DoHedged executes an action using speculative retries (hedging).
// It starts multiple attempts concurrently if previous ones take too long.
func DoHedged[T any](ctx context.Context, action func(context.Context) (T, error), opts ...Option) (T, error) {
return DoStateHedged(ctx, func(innerCtx context.Context, _ RetryState) (T, error) {
return action(innerCtx)
}, opts...)
}
// DoState executes a stateful action with retry logic using the provided options.
// The RetryState is passed to the closure, allowing it to adapt to failure history.
func DoState[T any](ctx context.Context, action func(context.Context, RetryState) (T, error), opts ...Option) (T, error) {
c := DefaultConfig()
for _, opt := range opts {
opt(c)
}
var result T
err := c.execute(ctx, func(innerCtx context.Context, state RetryState) error {
var innerErr error
result, innerErr = action(innerCtx, state)
return innerErr
}, nil)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) (T, error)); ok {
return f(ctx, err)
}
}
return result, err
}
// DoStateHedged executes a stateful action with speculative retries (hedging).
func DoStateHedged[T any](ctx context.Context, action func(context.Context, RetryState) (T, error), opts ...Option) (T, error) {
c := DefaultConfig()
for _, opt := range opts {
opt(c)
}
var result T
var once sync.Once
err := c.executeHedged(ctx, func(innerCtx context.Context, state RetryState) error {
val, innerErr := action(innerCtx, state)
if innerErr == nil {
once.Do(func() {
result = val
})
}
return innerErr
}, nil)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) (T, error)); ok {
return f(ctx, err)
}
}
return result, err
}
// DoErr executes an action with retry logic using the provided options.
// This function handles functions returning only error.
func DoErr(ctx context.Context, action func(context.Context) error, opts ...Option) error {
return DoErrState(ctx, func(innerCtx context.Context, _ RetryState) error {
return action(innerCtx)
}, opts...)
}
// DoErrHedged executes an action using speculative retries (hedging).
func DoErrHedged(ctx context.Context, action func(context.Context) error, opts ...Option) error {
return DoErrStateHedged(ctx, func(innerCtx context.Context, _ RetryState) error {
return action(innerCtx)
}, opts...)
}
// DoErrState executes a stateful action with retry logic using the provided options.
func DoErrState(ctx context.Context, action func(context.Context, RetryState) error, opts ...Option) error {
c := DefaultConfig()
for _, opt := range opts {
opt(c)
}
err := c.execute(ctx, action, nil)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) error); ok {
return f(ctx, err)
}
}
return err
}
// DoErrStateHedged executes a stateful action with speculative retries (hedging).
func DoErrStateHedged(ctx context.Context, action func(context.Context, RetryState) error, opts ...Option) error {
c := DefaultConfig()
for _, opt := range opts {
opt(c)
}
err := c.executeHedged(ctx, action, nil)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) error); ok {
return f(ctx, err)
}
}
return err
}
// New returns a new Retryer pre-configured with the provided options.
// This is useful for dependency injection and reusable resilience clients.
func New(opts ...Option) Retryer {
c := DefaultConfig()
for _, opt := range opts {
opt(c)
}
return c
}
// Do satisfies the Retryer interface. Note: returns any for interface compliance.
func (c *Config) Do(ctx context.Context, action func(context.Context) (any, error)) (any, error) {
var result any
err := c.execute(ctx, func(innerCtx context.Context, state RetryState) error {
var innerErr error
result, innerErr = action(innerCtx)
return innerErr
}, nil)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) (any, error)); ok {
return f(ctx, err)
}
}
return result, err
}
// DoHedged satisfies the Retryer interface.
func (c *Config) DoHedged(ctx context.Context, action func(context.Context) (any, error)) (any, error) {
var result any
var once sync.Once
err := c.executeHedged(ctx, func(innerCtx context.Context, state RetryState) error {
val, innerErr := action(innerCtx)
if innerErr == nil {
once.Do(func() {
result = val
})
}
return innerErr
}, nil)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) (any, error)); ok {
return f(ctx, err)
}
}
return result, err
}
// DoErr satisfies the Retryer interface.
func (c *Config) DoErr(ctx context.Context, action func(context.Context) error) error {
err := c.execute(ctx, nil, action)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) error); ok {
return f(ctx, err)
}
}
return err
}
// DoErrHedged satisfies the Retryer interface.
func (c *Config) DoErrHedged(ctx context.Context, action func(context.Context) error) error {
err := c.executeHedged(ctx, nil, action)
if err != nil && c.Fallback != nil {
if f, ok := c.Fallback.(func(context.Context, error) error); ok {
return f(ctx, err)
}
}
return err
}
// DefaultConfig returns a reasonable production-grade configuration.
func DefaultConfig() *Config {
return &Config{
MaxAttempts: 5,
BaseDelay: 100 * time.Millisecond,
MaxDelay: 30 * time.Second,
Backoff: NewFullJitter(100*time.Millisecond, 30*time.Second),
Policy: &retryPolicy{},
MinDeadlineThreshold: 5 * time.Millisecond,
}
}
// doAction is an internal type to support both stateless and stateful execution.
type doAction func(context.Context, RetryState) error
// execute executes the action with the provided configuration and context.
func (c *Config) execute(ctx context.Context, action doAction, simpleAction func(context.Context) error) error {
h := c.getComposedPipeline()
return h(ctx, RetryState{
Name: c.Name,
MaxAttempts: c.MaxAttempts,
action: action,
simpleAction: simpleAction,
})
}
// getComposedPipeline returns the cached composed pipeline or builds it if not present.
func (c *Config) getComposedPipeline() doAction {
c.mu.RLock()
if c.composedPipeline != nil {
defer c.mu.RUnlock()
return c.composedPipeline
}
c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.composedPipeline != nil {
return c.composedPipeline
}
if len(c.pipeline) == 0 {
c.buildDefaultPipeline()
}
h := terminalAction
for i := len(c.pipeline) - 1; i >= 0; i-- {
h = c.pipeline[i](h)
}
c.composedPipeline = h
return h
}
// buildDefaultPipeline builds the legacy hardcoded pipeline order.
// Order: Bulkhead -> Retry ( CircuitBreaker -> Instrumenter -> PanicRecovery )
func (c *Config) buildDefaultPipeline() {
if c.RateLimiter != nil {
c.pipeline = append(c.pipeline, c.rateLimiterMiddleware())
}
if c.AdaptiveLimiter != nil {
c.pipeline = append(c.pipeline, c.adaptiveLimiterMiddleware())
}
if c.Bulkhead != nil {
c.pipeline = append(c.pipeline, c.bulkheadMiddleware())
}
if c.PriorityBulkhead != nil {
c.pipeline = append(c.pipeline, c.priorityBulkheadMiddleware())
}
// Retry is the primary driver in the legacy model.
c.pipeline = append(c.pipeline, c.retryMiddleware())
c.pipeline = append(c.pipeline, c.deadlineMiddleware())
if c.Timeout > 0 {
c.pipeline = append(c.pipeline, c.timeoutMiddleware(c.Timeout))
}
if c.CircuitBreaker != nil {
c.pipeline = append(c.pipeline, c.circuitBreakerMiddleware())
}
c.pipeline = append(c.pipeline, c.instrumenterMiddleware())
if c.RecoverPanics {
c.pipeline = append(c.pipeline, c.panicRecoveryMiddleware())
}
if c.Chaos != nil {
c.pipeline = append(c.pipeline, c.chaosMiddleware())
}
}
func (c *Config) chaosMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if c.Chaos == nil {
return next(ctx, state)
}
return c.Chaos.Execute(ctx, func() error {
return next(ctx, state)
})
}
}
}
func (c *Config) deadlineMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if deadline, ok := ctx.Deadline(); ok {
remaining := time.Until(deadline)
if remaining <= c.MinDeadlineThreshold {
return context.DeadlineExceeded
}
}
return next(ctx, state)
}
}
}
func (c *Config) rateLimiterMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if c.RateLimiter == nil {
return next(ctx, state)
}
if !c.RateLimiter.Acquire(ctx) {
if c.Instrumenter != nil {
c.Instrumenter.OnRateLimitExceeded(ctx, state)
}
return ErrRateLimitExceeded
}
return next(ctx, state)
}
}
}
func (c *Config) timeoutMiddleware(timeout time.Duration) middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return next(ctx, state)
}
}
}
func (c *Config) retryMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
var errs []error
start := time.Now()
for attempt := uint(0); attempt < c.MaxAttempts; attempt++ {
state.Attempt = attempt
if len(errs) > 0 {
state.LastError = errs[len(errs)-1]
}
state.TotalDuration = time.Since(start)
err := next(ctx, state)
// Success terminates the loop.
if err == nil {
if c.AdaptiveBucket != nil {
c.AdaptiveBucket.AddSuccessToken()
}
return nil
}
errs = append(errs, err)
// Check for circuit open to avoid retries.
if errors.Is(err, circuit.ErrCircuitOpen) {
return errors.Join(errs...)
}
// Check if we should retry based on the error policy.
if !c.Policy.shouldRetry(err) {
return errors.Join(errs...)
}
// If this was the last attempt, don't sleep.
if attempt+1 >= c.MaxAttempts {
break
}
// Check adaptive bucket before committing to next attempt.
if c.AdaptiveBucket != nil && !c.AdaptiveBucket.AcquireRetryToken() {
break
}
// Calculate the next delay.
delay := c.Backoff.Next(attempt)
// Check for Retry-After override.
var retryAfter RetryAfterError
if errors.As(err, &retryAfter) {
if retryAfter.CancelAllRetries() {
return errors.Join(errs...)
}
delay = retryAfter.RetryAfter()
}
// Sleep safely with context awareness.
if err := sleep(ctx, delay); err != nil {
// Context canceled during sleep.
return errors.Join(append(errs, err)...)
}
}
return errors.Join(errs...)
}
}
}
func (c *Config) circuitBreakerMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if c.CircuitBreaker == nil {
return next(ctx, state)
}
return c.CircuitBreaker.Execute(ctx, func() error {
return next(ctx, state)
})
}
}
}
func (c *Config) bulkheadMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if c.Bulkhead == nil {
return next(ctx, state)
}
err := c.Bulkhead.Execute(ctx, func() error {
return next(ctx, state)
})
if errors.Is(err, ErrBulkheadFull) && c.Instrumenter != nil {
c.Instrumenter.OnBulkheadFull(ctx, state)
}
return err
}
}
}
func (c *Config) priorityBulkheadMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if c.PriorityBulkhead == nil {
return next(ctx, state)
}
err := c.PriorityBulkhead.Execute(ctx, func() error {
return next(ctx, state)
})
if (errors.Is(err, ErrBulkheadFull) || errors.Is(err, ErrShedLoad)) && c.Instrumenter != nil {
c.Instrumenter.OnBulkheadFull(ctx, state)
}
return err
}
}
}
func (c *Config) instrumenterMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
if c.Instrumenter == nil {
return next(ctx, state)
}
ctx = c.Instrumenter.BeforeAttempt(ctx, state)
err := next(ctx, state)
state.LastError = err
c.Instrumenter.AfterAttempt(ctx, state)
return err
}
}
}
func (c *Config) panicRecoveryMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) (err error) {
defer func() {
if r := recover(); r != nil {
err = &PanicError{
Value: r,
StackTrace: string(debug.Stack()),
}
}
}()
return next(ctx, state)
}
}
}
func (c *Config) fallbackMiddleware() middleware {
return func(next doAction) doAction {
return func(ctx context.Context, state RetryState) error {
err := next(ctx, state)
if err != nil && c.Fallback != nil {
// Fallback for DoErr
if f, ok := c.Fallback.(func(context.Context, error) error); ok {
return f(ctx, err)
}
// Note: Fallback for value-returning Do is handled at the top-level
// in DoState to preserve type safety without reflection.
}
return err
}
}
}
// executeHedged executes the action using speculative retries (hedging).
func (c *Config) executeHedged(ctx context.Context, action doAction, simpleAction func(context.Context) error) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
results := make(chan error, c.MaxAttempts)
errs := make([]error, 0, c.MaxAttempts)
var inFlight int
var attemptsStarted uint
start := time.Now()
// Build attempt pipeline (everything except the outer retry loop).
// For hedging, the retry loop is handled by executeHedged itself.
h := terminalAction
if c.RateLimiter != nil {
h = c.rateLimiterMiddleware()(h)
}
if c.Bulkhead != nil {
h = c.bulkheadMiddleware()(h)
}
if c.PriorityBulkhead != nil {
h = c.priorityBulkheadMiddleware()(h)
}
if c.Timeout > 0 {
h = c.timeoutMiddleware(c.Timeout)(h)
}
h = c.deadlineMiddleware()(h)
if c.CircuitBreaker != nil {
h = c.circuitBreakerMiddleware()(h)
}
h = c.instrumenterMiddleware()(h)
if c.RecoverPanics {
h = c.panicRecoveryMiddleware()(h)
}
if c.Chaos != nil {
h = c.chaosMiddleware()(h)
}
for {
if attemptsStarted < c.MaxAttempts {
canStart := true
if attemptsStarted > 0 && c.AdaptiveBucket != nil {
canStart = c.AdaptiveBucket.AcquireRetryToken()
}
if canStart {
attempt := attemptsStarted
attemptsStarted++
inFlight++
go func(a uint) {
state := RetryState{
Name: c.Name,
Attempt: a,
MaxAttempts: c.MaxAttempts,
TotalDuration: time.Since(start),
NextDelay: c.HedgingDelay,
action: action,
simpleAction: simpleAction,
}
err := h(ctx, state)
select {
case results <- err:
case <-ctx.Done():
}
}(attempt)
} else {
attemptsStarted = c.MaxAttempts
}
}
if attemptsStarted >= c.MaxAttempts && inFlight == 0 {
break
}
var timerCh <-chan time.Time
var timer *time.Timer
if attemptsStarted < c.MaxAttempts {
timer = time.NewTimer(c.HedgingDelay)
timerCh = timer.C
}
select {
case <-ctx.Done():
if timer != nil {
timer.Stop()
}
return errors.Join(append(errs, ctx.Err())...)
case err := <-results:
if timer != nil {
timer.Stop()
}
inFlight--
if err == nil {
cancel()
if c.AdaptiveBucket != nil {
c.AdaptiveBucket.AddSuccessToken()
}
return nil
}
errs = append(errs, err)
// Check for circuit open to avoid further retries.
if errors.Is(err, circuit.ErrCircuitOpen) {
cancel()
return errors.Join(errs...)
}
// Check for pushback signal to cancel all retries.
var retryAfter RetryAfterError
if errors.As(err, &retryAfter) && retryAfter.CancelAllRetries() {
cancel()
return errors.Join(errs...)
}
// If error is not retryable, cancel all and return.
if !c.Policy.shouldRetry(err) {
cancel()
return errors.Join(errs...)
}
// If no more attempts are in-flight, start next one immediately.
if inFlight == 0 && attemptsStarted < c.MaxAttempts {
continue
}
case <-timerCh:
// Hedging delay reached, start next attempt if available.
}
}
return errors.Join(errs...)
}
type contextKey string
const bypassDelayKey contextKey = "resile_bypass_delay"
// WithTestingBypass returns a new context that signals the retry loop to skip all sleep delays.
// This is intended for use in unit tests to prevent CI pipelines from being slowed down by backoff.
func WithTestingBypass(ctx context.Context) context.Context {
return context.WithValue(ctx, bypassDelayKey, true)
}
// sleep provides a memory-safe, context-aware delay using time.NewTimer.
func sleep(ctx context.Context, delay time.Duration) error {
if delay <= 0 {
return nil
}
// Check for testing bypass.
if bypass, ok := ctx.Value(bypassDelayKey).(bool); ok && bypass {
return nil
}
timer := time.NewTimer(delay)
defer func() {
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return nil
}
}