-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
402 lines (335 loc) · 9.63 KB
/
example_test.go
File metadata and controls
402 lines (335 loc) · 9.63 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
package retry_test
import (
"context"
"errors"
"fmt"
"time"
"github.com/bjaus/retry"
)
// ExampleDo demonstrates the simplest usage with the global Do function.
func ExampleDo() {
attempts := 0
err := retry.Do(context.Background(), func(ctx context.Context) error {
attempts++
if attempts < 3 {
return errors.New("temporary failure")
}
return nil
},
retry.WithMaxAttempts(5),
retry.WithBackoff(retry.Constant(time.Millisecond)),
)
fmt.Println("Error:", err)
fmt.Println("Attempts:", attempts)
// Output:
// Error: <nil>
// Attempts: 3
}
// ExampleNew demonstrates creating a reusable policy.
func ExampleNew() {
policy := retry.New(
retry.WithMaxAttempts(3),
retry.WithBackoff(retry.Constant(time.Millisecond)),
)
attempts := 0
err := policy.Do(context.Background(), func(ctx context.Context) error {
attempts++
return errors.New("always fails")
})
fmt.Println("Error:", err)
fmt.Println("Attempts:", attempts)
// Output:
// Error: always fails
// Attempts: 3
}
// ExampleNever demonstrates a policy that does not retry.
func ExampleNever() {
policy := retry.Never()
attempts := 0
_ = policy.Do(context.Background(), func(ctx context.Context) error {
attempts++
return errors.New("fail")
})
fmt.Println("Attempts:", attempts)
// Output:
// Attempts: 1
}
// ExampleStop demonstrates signaling a non-retryable error.
func ExampleStop() {
notFound := errors.New("not found")
attempts := 0
err := retry.Do(context.Background(), func(ctx context.Context) error {
attempts++
return retry.Stop(notFound)
},
retry.WithMaxAttempts(5),
retry.WithBackoff(retry.Constant(time.Millisecond)),
)
fmt.Println("Error:", err)
fmt.Println("Attempts:", attempts)
// Output:
// Error: not found
// Attempts: 1
}
// ExampleIf demonstrates conditional retry based on error type.
func ExampleIf() {
transient := errors.New("transient error")
permanent := errors.New("permanent error")
attempts := 0
err := retry.Do(context.Background(), func(ctx context.Context) error {
attempts++
if attempts < 3 {
return transient
}
return permanent
},
retry.WithMaxAttempts(10),
retry.WithBackoff(retry.Constant(time.Millisecond)),
retry.If(func(err error) bool {
return errors.Is(err, transient)
}),
)
fmt.Println("Error:", err)
fmt.Println("Attempts:", attempts)
// Output:
// Error: permanent error
// Attempts: 3
}
// ExampleIfNot demonstrates skipping specific errors from retry.
func ExampleIfNot() {
validationErr := errors.New("validation error")
attempts := 0
err := retry.Do(context.Background(), func(ctx context.Context) error {
attempts++
if attempts == 2 {
return validationErr // Don't retry this
}
return errors.New("transient")
},
retry.WithMaxAttempts(10),
retry.WithBackoff(retry.Constant(time.Millisecond)),
retry.IfNot(func(err error) bool {
return errors.Is(err, validationErr)
}),
)
fmt.Println("Error:", err)
fmt.Println("Attempts:", attempts)
// Output:
// Error: validation error
// Attempts: 2
}
// ExampleNot demonstrates inverting a condition.
func ExampleNot() {
isTimeout := func(err error) bool {
return err.Error() == "timeout"
}
// Retry everything EXCEPT timeouts
notTimeout := retry.Not(isTimeout)
fmt.Println("timeout matches:", isTimeout(errors.New("timeout")))
fmt.Println("timeout matches Not:", notTimeout(errors.New("timeout")))
fmt.Println("other matches Not:", notTimeout(errors.New("other")))
// Output:
// timeout matches: true
// timeout matches Not: false
// other matches Not: true
}
// ExampleOnRetry demonstrates the retry hook for logging.
func ExampleOnRetry() {
attempts := 0
retryCount := 0
_ = retry.Do(context.Background(), func(ctx context.Context) error {
attempts++
return errors.New("fail")
},
retry.WithMaxAttempts(3),
retry.WithBackoff(retry.Constant(time.Millisecond)),
retry.OnRetry(func(ctx context.Context, attempt int, err error, delay time.Duration) {
retryCount++
fmt.Printf("Retry %d: %v\n", attempt, err)
}),
)
fmt.Println("Total retries:", retryCount)
// Output:
// Retry 1: fail
// Retry 2: fail
// Total retries: 2
}
// ExampleOnSuccess demonstrates the success hook.
func ExampleOnSuccess() {
attempts := 0
_ = retry.Do(context.Background(), func(ctx context.Context) error {
attempts++
if attempts < 3 {
return errors.New("not yet")
}
return nil
},
retry.WithMaxAttempts(5),
retry.WithBackoff(retry.Constant(time.Millisecond)),
retry.OnSuccess(func(ctx context.Context, attempts int) {
fmt.Printf("Succeeded on attempt %d\n", attempts)
}),
)
// Output:
// Succeeded on attempt 3
}
// ExampleOnExhausted demonstrates the exhausted hook.
func ExampleOnExhausted() {
_ = retry.Do(context.Background(), func(ctx context.Context) error {
return errors.New("always fails")
},
retry.WithMaxAttempts(3),
retry.WithBackoff(retry.Constant(time.Millisecond)),
retry.OnExhausted(func(ctx context.Context, attempts int, err error) {
fmt.Printf("Exhausted after %d attempts: %v\n", attempts, err)
}),
)
// Output:
// Exhausted after 3 attempts: always fails
}
// ExampleWithAllErrors demonstrates collecting all errors.
func ExampleWithAllErrors() {
attempt := 0
err := retry.Do(context.Background(), func(ctx context.Context) error {
attempt++
return fmt.Errorf("error %d", attempt)
},
retry.WithMaxAttempts(3),
retry.WithBackoff(retry.Constant(time.Millisecond)),
retry.WithAllErrors(),
)
fmt.Println("Contains error 1:", errors.Is(err, fmt.Errorf("error 1")))
fmt.Println("Error string contains all:", err != nil)
// Output:
// Contains error 1: false
// Error string contains all: true
}
// ExampleConstant demonstrates constant backoff.
func ExampleConstant() {
b := retry.Constant(100 * time.Millisecond)
fmt.Println("Attempt 1:", b.Delay(1))
fmt.Println("Attempt 2:", b.Delay(2))
fmt.Println("Attempt 5:", b.Delay(5))
// Output:
// Attempt 1: 100ms
// Attempt 2: 100ms
// Attempt 5: 100ms
}
// ExampleLinear demonstrates linear backoff.
func ExampleLinear() {
b := retry.Linear(100 * time.Millisecond)
fmt.Println("Attempt 1:", b.Delay(1))
fmt.Println("Attempt 2:", b.Delay(2))
fmt.Println("Attempt 5:", b.Delay(5))
// Output:
// Attempt 1: 100ms
// Attempt 2: 200ms
// Attempt 5: 500ms
}
// ExampleExponential demonstrates exponential backoff.
func ExampleExponential() {
b := retry.Exponential(100 * time.Millisecond)
fmt.Println("Attempt 1:", b.Delay(1))
fmt.Println("Attempt 2:", b.Delay(2))
fmt.Println("Attempt 3:", b.Delay(3))
fmt.Println("Attempt 4:", b.Delay(4))
// Output:
// Attempt 1: 100ms
// Attempt 2: 200ms
// Attempt 3: 400ms
// Attempt 4: 800ms
}
// ExampleWithCap demonstrates capping backoff delays.
func ExampleWithCap() {
b := retry.WithCap(500*time.Millisecond, retry.Exponential(100*time.Millisecond))
fmt.Println("Attempt 1:", b.Delay(1))
fmt.Println("Attempt 2:", b.Delay(2))
fmt.Println("Attempt 3:", b.Delay(3))
fmt.Println("Attempt 4:", b.Delay(4)) // Would be 800ms, capped to 500ms
fmt.Println("Attempt 5:", b.Delay(5)) // Would be 1.6s, capped to 500ms
// Output:
// Attempt 1: 100ms
// Attempt 2: 200ms
// Attempt 3: 400ms
// Attempt 4: 500ms
// Attempt 5: 500ms
}
// ExampleWithMin demonstrates minimum backoff delays.
func ExampleWithMin() {
b := retry.WithMin(150*time.Millisecond, retry.Linear(50*time.Millisecond))
fmt.Println("Attempt 1:", b.Delay(1)) // 50ms -> 150ms (min)
fmt.Println("Attempt 2:", b.Delay(2)) // 100ms -> 150ms (min)
fmt.Println("Attempt 3:", b.Delay(3)) // 150ms (at min)
fmt.Println("Attempt 4:", b.Delay(4)) // 200ms (above min)
// Output:
// Attempt 1: 150ms
// Attempt 2: 150ms
// Attempt 3: 150ms
// Attempt 4: 200ms
}
// ExampleBackoffFunc demonstrates creating a custom backoff strategy.
func ExampleBackoffFunc() {
// Quadratic backoff: delay = base * attempt^2
b := retry.BackoffFunc(func(attempt int) time.Duration {
return time.Duration(attempt*attempt) * 10 * time.Millisecond
})
fmt.Println("Attempt 1:", b.Delay(1))
fmt.Println("Attempt 2:", b.Delay(2))
fmt.Println("Attempt 3:", b.Delay(3))
fmt.Println("Attempt 4:", b.Delay(4))
// Output:
// Attempt 1: 10ms
// Attempt 2: 40ms
// Attempt 3: 90ms
// Attempt 4: 160ms
}
// Example_composedBackoff demonstrates composing multiple backoff wrappers.
func Example_composedBackoff() {
// Exponential backoff, capped at 1s, with minimum 50ms
b := retry.WithMin(50*time.Millisecond,
retry.WithCap(1*time.Second,
retry.Exponential(10*time.Millisecond),
),
)
fmt.Println("Attempt 1:", b.Delay(1)) // 10ms -> 50ms (min)
fmt.Println("Attempt 2:", b.Delay(2)) // 20ms -> 50ms (min)
fmt.Println("Attempt 5:", b.Delay(5)) // 160ms
fmt.Println("Attempt 10:", b.Delay(10)) // 5.12s -> 1s (cap)
// Output:
// Attempt 1: 50ms
// Attempt 2: 50ms
// Attempt 5: 160ms
// Attempt 10: 1s
}
// Example_dependencyInjection demonstrates the recommended DI pattern.
func Example_dependencyInjection() {
// === Wire-up time (e.g., in main or DI container) ===
policy := retry.New(
retry.WithMaxAttempts(5),
retry.WithBackoff(retry.Constant(time.Millisecond)),
)
// === Call site (in application code) ===
// The caller doesn't know or care about the retry budget.
// It only controls which errors to retry and what to log.
attempts := 0
var retried bool
err := policy.Do(context.Background(), func(ctx context.Context) error {
attempts++
if attempts < 2 {
return errors.New("transient")
}
return nil
},
retry.If(func(err error) bool {
return err.Error() == "transient"
}),
retry.OnRetry(func(ctx context.Context, attempt int, err error, delay time.Duration) {
retried = true
}),
)
fmt.Println("Error:", err)
fmt.Println("Retried:", retried)
// Output:
// Error: <nil>
// Retried: true
}