-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
298 lines (261 loc) · 7.9 KB
/
example_test.go
File metadata and controls
298 lines (261 loc) · 7.9 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
package try_test
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
"github.com/nodivbyzero/try"
)
// ExampleDo shows the minimal usage: a function that succeeds on the first
// call requires no options at all.
func ExampleDo() {
ctx := context.Background()
val, err := try.Do(ctx, func(ctx context.Context) (string, error) {
return "hello", nil
})
fmt.Println(val, err)
// Output:
// hello <nil>
}
// ExampleDo_transientFailure demonstrates retrying a flaky operation.
// WithRetryIf restricts retries to known transient errors — a best practice
// for production code so that validation and auth failures fail fast.
func ExampleDo_transientFailure() {
ctx := context.Background()
attempt := 0
val, err := try.Do(ctx, func(ctx context.Context) (int, error) {
attempt++
if attempt < 3 {
return 0, errors.New("connection reset by peer")
}
return 42, nil
},
try.WithAttempts(5),
try.WithInitialDelay(time.Millisecond),
try.WithRetryIf(func(err error) bool {
return err.Error() == "connection reset by peer"
}),
)
fmt.Println(val, err)
// Output:
// 42 <nil>
}
// ExampleDo_permanentError shows how to stop the retry loop immediately for
// errors that will never succeed on retry (auth failures, bad input, etc.).
// Permanent unwraps transparently so errors.Is / errors.As work normally.
func ExampleDo_permanentError() {
ctx := context.Background()
calls := 0
_, err := try.Do(ctx, func(ctx context.Context) (string, error) {
calls++
return "", try.Permanent(errors.New("invalid API key"))
}, try.WithAttempts(5))
fmt.Println(calls, err)
// Output:
// 1 invalid API key
}
// ExampleDo_onRetry demonstrates the OnRetry callback, which fires before
// each wait and receives the attempt number, error, and upcoming delay.
// It is not called on the final attempt since no retry will follow.
func ExampleDo_onRetry() {
ctx := context.Background()
attempt := 0
try.Do(ctx, func(ctx context.Context) (int, error) { //nolint:errcheck
attempt++
if attempt < 3 {
return 0, errors.New("unavailable")
}
return 0, nil
},
try.WithAttempts(5),
try.WithInitialDelay(time.Millisecond),
try.WithOnRetry(func(info try.RetryInfo) {
slog.Info("retrying", "attempt", info.Attempt, "error", info.Err)
}),
)
fmt.Println("completed after", attempt, "attempts")
// Output:
// completed after 3 attempts
}
// rateLimitErr implements RetryAfterer to specify a custom wait duration.
type rateLimitErr struct{ wait time.Duration }
func (e rateLimitErr) Error() string { return "rate limited" }
func (e rateLimitErr) RetryAfter() time.Duration { return e.wait }
// ExampleDo_retryAfter shows how an error can specify its own wait duration
// via the RetryAfterer interface — useful for honouring HTTP 429 headers.
func ExampleDo_retryAfter() {
ctx := context.Background()
attempt := 0
val, err := try.Do(ctx, func(ctx context.Context) (string, error) {
attempt++
if attempt == 1 {
return "", rateLimitErr{wait: 5 * time.Millisecond}
}
return "ok", nil
}, try.WithAttempts(3))
fmt.Println(val, err)
// Output:
// ok <nil>
}
// ExampleDo_equalJitter shows the EqualJitter strategy, which guarantees at
// least half the computed backoff — useful when a minimum wait time matters.
func ExampleDo_equalJitter() {
ctx := context.Background()
attempt := 0
val, err := try.Do(ctx, func(ctx context.Context) (string, error) {
attempt++
if attempt < 3 {
return "", errors.New("service unavailable")
}
return "recovered", nil
},
try.WithAttempts(5),
try.WithInitialDelay(time.Millisecond),
try.WithMaxDelay(100*time.Millisecond),
try.WithJitter(try.EqualJitter),
)
fmt.Println(val, err)
// Output:
// recovered <nil>
}
// ExampleDo_infiniteRetry shows WithInfiniteRetry, which retries until the
// function succeeds, a Permanent error is returned, or the context is cancelled.
// Always pair it with a context deadline.
func ExampleDo_infiniteRetry() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
attempt := 0
val, err := try.Do(ctx, func(ctx context.Context) (string, error) {
attempt++
if attempt < 4 {
return "", errors.New("not ready")
}
return "ready", nil
},
try.WithInfiniteRetry(),
try.WithInitialDelay(time.Millisecond),
)
fmt.Println(val, err)
// Output:
// ready <nil>
}
// ExampleDo_allErrors shows WithAllErrors, which collects every attempt error
// into *AttemptErrors. errors.Is and errors.As traverse the full history.
func ExampleDo_allErrors() {
ctx := context.Background()
sentinel := errors.New("quota exceeded")
attempt := 0
_, err := try.Do(ctx, func(ctx context.Context) (int, error) {
attempt++
if attempt == 2 {
return 0, sentinel // one specific error among several
}
return 0, errors.New("generic failure")
},
try.WithAttempts(3),
try.WithAllErrors(),
try.WithInitialDelay(time.Millisecond),
)
var ae *try.AttemptErrors
fmt.Println(errors.As(err, &ae)) // *AttemptErrors accessible
fmt.Println(errors.Is(err, sentinel)) // sentinel reachable anywhere in history
// Output:
// true
// true
}
// ExampleDo_withTimeout shows WithTimeout, which sets a per-attempt deadline
// independent of the overall context deadline.
func ExampleDo_withTimeout() {
// Overall budget: 5s. Each attempt gets at most 100ms.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
attempt := 0
val, err := try.Do(ctx, func(ctx context.Context) (string, error) {
attempt++
if attempt < 3 {
// Simulate a slow response that exceeds the per-attempt timeout.
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(10 * time.Second):
return "", errors.New("slow")
}
}
return "fast enough", nil
},
try.WithAttempts(5),
try.WithTimeout(10*time.Millisecond),
try.WithInitialDelay(time.Millisecond),
try.WithRetryIf(func(err error) bool {
return errors.Is(err, context.DeadlineExceeded)
}),
)
fmt.Println(val, err)
// Output:
// fast enough <nil>
}
// ExampleDo_attemptsForError shows WithAttemptsForError, which caps retries
// for a specific error independently of the global attempt budget.
func ExampleDo_attemptsForError() {
ctx := context.Background()
var ErrRateLimit = errors.New("rate limited")
calls := 0
_, err := try.Do(ctx, func(ctx context.Context) (int, error) {
calls++
return 0, ErrRateLimit
},
try.WithAttempts(10), // global budget: 10
try.WithAttemptsForError(2, ErrRateLimit), // but stop after 2 rate-limit hits
try.WithInitialDelay(time.Millisecond),
)
fmt.Println(calls, errors.Is(err, ErrRateLimit))
// Output:
// 2 true
}
// ExampleDo_delayFunc shows WithDelayFunc replacing the built-in backoff
// with a custom linear schedule.
func ExampleDo_delayFunc() {
ctx := context.Background()
var observedAttempts []int
attempt := 0
try.Do(ctx, func(ctx context.Context) (int, error) { //nolint:errcheck
attempt++
if attempt < 3 {
return 0, errors.New("fail")
}
return 0, nil
},
try.WithAttempts(5),
try.WithDelayFunc(func(a int, _ error) time.Duration {
observedAttempts = append(observedAttempts, a)
return time.Duration(a) * time.Millisecond // linear: 1ms, 2ms, …
}),
)
fmt.Println(observedAttempts)
// Output:
// [1 2]
}
// ExampleIsPermanent shows that IsPermanent reports whether an error was
// wrapped with Permanent, even through additional wrapping layers.
func ExampleIsPermanent() {
base := errors.New("fatal")
perm := try.Permanent(base)
wrapped := fmt.Errorf("outer: %w", perm)
fmt.Println(try.IsPermanent(perm))
fmt.Println(try.IsPermanent(wrapped))
fmt.Println(try.IsPermanent(base))
// Output:
// true
// true
// false
}
// ExamplePermanent shows that Permanent unwraps cleanly, so the original
// error remains inspectable via errors.Is after the loop exits.
func ExamplePermanent() {
sentinel := errors.New("fatal")
wrapped := try.Permanent(sentinel)
fmt.Println(errors.Is(wrapped, sentinel))
// Output:
// true
}