-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathretry.go
77 lines (66 loc) · 1.86 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
package utils
import (
"context"
"errors"
"fmt"
"slices"
"time"
)
// RetryError is emitted by RetryNTimes if all the attempts fail. It unwraps to the last error from retrying.
type RetryError struct {
inner error
attempts int
}
func (e *RetryError) Error() string {
return fmt.Sprintf("failed after %d retry attempts: %v", e.attempts, e.inner)
}
func (e *RetryError) Unwrap() error {
return e.inner
}
// RetryNTimesWithSleep will run `fallibleFunc` `retryAttempts` times before failing with the last error it got from the function.
// If `retryableErrors` is supplied, only those errors will be retried.
// It will wait for `retryDelay` between attempts.
func RetryNTimesWithSleep[T any](
ctx context.Context,
fallibleFunc func() (T, error),
retryAttempts int,
retryDelay time.Duration,
retryableErrors ...error,
) (T, error) {
var lastError error
var emptyT T
for range retryAttempts {
val, err := fallibleFunc()
if err == nil || (len(retryableErrors) != 0 &&
!slices.ContainsFunc(retryableErrors, func(target error) bool { return errors.Is(err, target) })) {
return val, err
}
lastError = err
if ctx.Err() != nil {
return emptyT, ctx.Err()
}
select {
case <-ctx.Done():
return emptyT, ctx.Err()
case <-time.After(retryDelay):
}
}
return emptyT, &RetryError{attempts: retryAttempts, inner: lastError}
}
// RetryNTimes will run `fallibleFunc` `retryAttempts` times before failing with the last error it got from the function.
// If `retryableErrors` is supplied, only those errors will be retried.
// It will wait 1 second between attempts. Use RetryNTimesWithSleep to change this.
func RetryNTimes[T any](
ctx context.Context,
fallibleFunc func() (T, error),
retryAttempts int,
retryableErrors ...error,
) (T, error) {
return RetryNTimesWithSleep(
ctx,
fallibleFunc,
retryAttempts,
time.Second,
retryableErrors...,
)
}