-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
retry.go
216 lines (171 loc) · 7.68 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
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
package tfresource
import (
"context"
"errors"
"math/rand"
"sync"
"time"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
// Retryable is a function that is used to decide if a function's error is retryable or not.
// The error argument can be `nil`.
// If the error is retryable, returns a bool value of `true` and an error (not necessarily the error passed as the argument).
// If the error is not retryable, returns a bool value of `false` and either no error (success state) or an error (not necessarily the error passed as the argument).
type Retryable func(error) (bool, error)
// RetryWhenContext retries the function `f` when the error it returns satisfies `predicate`.
// `f` is retried until `timeout` expires.
func RetryWhenContext(ctx context.Context, timeout time.Duration, f func() (interface{}, error), retryable Retryable) (interface{}, error) {
var output interface{}
err := resource.Retry(timeout, func() *resource.RetryError { // nosemgrep:ci.helper-schema-resource-Retry-without-TimeoutError-check
var err error
var retry bool
output, err = f()
retry, err = retryable(err)
if retry {
return resource.RetryableError(err)
}
if err != nil {
return resource.NonRetryableError(err)
}
return nil
})
if TimedOut(err) {
output, err = f()
}
if err != nil {
return nil, err
}
return output, nil
}
// RetryWhen retries the function `f` when the error it returns satisfies `predicate`.
// `f` is retried until `timeout` expires.
func RetryWhen(timeout time.Duration, f func() (interface{}, error), retryable Retryable) (interface{}, error) {
return RetryWhenContext(context.Background(), timeout, f, retryable)
}
// RetryWhenAWSErrCodeEqualsContext retries the specified function when it returns one of the specified AWS error code.
func RetryWhenAWSErrCodeEqualsContext(ctx context.Context, timeout time.Duration, f func() (interface{}, error), codes ...string) (interface{}, error) { // nosemgrep:ci.aws-in-func-name
return RetryWhenContext(ctx, timeout, f, func(err error) (bool, error) {
if tfawserr.ErrCodeEquals(err, codes...) {
return true, err
}
return false, err
})
}
// RetryWhenAWSErrCodeEquals retries the specified function when it returns one of the specified AWS error code.
func RetryWhenAWSErrCodeEquals(timeout time.Duration, f func() (interface{}, error), codes ...string) (interface{}, error) { // nosemgrep:ci.aws-in-func-name
return RetryWhenAWSErrCodeEqualsContext(context.Background(), timeout, f, codes...)
}
// RetryWhenAWSErrMessageContainsContext retries the specified function when it returns an AWS error containing the specified message.
func RetryWhenAWSErrMessageContainsContext(ctx context.Context, timeout time.Duration, f func() (interface{}, error), code, message string) (interface{}, error) { // nosemgrep:ci.aws-in-func-name
return RetryWhenContext(ctx, timeout, f, func(err error) (bool, error) {
if tfawserr.ErrMessageContains(err, code, message) {
return true, err
}
return false, err
})
}
// RetryWhenAWSErrMessageContains retries the specified function when it returns an AWS error containing the specified message.
func RetryWhenAWSErrMessageContains(timeout time.Duration, f func() (interface{}, error), code, message string) (interface{}, error) { // nosemgrep:ci.aws-in-func-name
return RetryWhenAWSErrMessageContainsContext(context.Background(), timeout, f, code, message)
}
var errFoundResource = errors.New(`found resource`)
// RetryUntilNotFoundContext retries the specified function until it returns a resource.NotFoundError.
func RetryUntilNotFoundContext(ctx context.Context, timeout time.Duration, f func() (interface{}, error)) (interface{}, error) {
return RetryWhenContext(ctx, timeout, f, func(err error) (bool, error) {
if NotFound(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, errFoundResource
})
}
// RetryUntilNotFound retries the specified function until it returns a resource.NotFoundError.
func RetryUntilNotFound(timeout time.Duration, f func() (interface{}, error)) (interface{}, error) {
return RetryUntilNotFoundContext(context.Background(), timeout, f)
}
// RetryWhenNotFoundContext retries the specified function when it returns a resource.NotFoundError.
func RetryWhenNotFoundContext(ctx context.Context, timeout time.Duration, f func() (interface{}, error)) (interface{}, error) {
return RetryWhenContext(ctx, timeout, f, func(err error) (bool, error) {
if NotFound(err) {
return true, err
}
return false, err
})
}
// RetryWhenNotFound retries the specified function when it returns a resource.NotFoundError.
func RetryWhenNotFound(timeout time.Duration, f func() (interface{}, error)) (interface{}, error) {
return RetryWhenNotFoundContext(context.Background(), timeout, f)
}
// RetryWhenNewResourceNotFoundContext retries the specified function when it returns a resource.NotFoundError and `isNewResource` is true.
func RetryWhenNewResourceNotFoundContext(ctx context.Context, timeout time.Duration, f func() (interface{}, error), isNewResource bool) (interface{}, error) {
return RetryWhenContext(ctx, timeout, f, func(err error) (bool, error) {
if isNewResource && NotFound(err) {
return true, err
}
return false, err
})
}
// RetryWhenNewResourceNotFound retries the specified function when it returns a resource.NotFoundError and `isNewResource` is true.
func RetryWhenNewResourceNotFound(timeout time.Duration, f func() (interface{}, error), isNewResource bool) (interface{}, error) {
return RetryWhenNewResourceNotFoundContext(context.Background(), timeout, f, isNewResource)
}
// RetryConfigContext allows configuration of StateChangeConf's various time arguments.
// This is especially useful for AWS services that are prone to throttling, such as Route53, where
// the default durations cause problems. To not use a StateChangeConf argument and revert to the
// default, pass in a zero value (i.e., 0*time.Second).
func RetryConfigContext(ctx context.Context, delay time.Duration, delayRand time.Duration, minTimeout time.Duration, pollInterval time.Duration, timeout time.Duration, f resource.RetryFunc) error {
// These are used to pull the error out of the function; need a mutex to
// avoid a data race.
var resultErr error
var resultErrMu sync.Mutex
c := &resource.StateChangeConf{
Pending: []string{"retryableerror"},
Target: []string{"success"},
Timeout: timeout,
Refresh: func() (interface{}, string, error) {
rerr := f()
resultErrMu.Lock()
defer resultErrMu.Unlock()
if rerr == nil {
resultErr = nil
return 42, "success", nil
}
resultErr = rerr.Err
if rerr.Retryable {
return 42, "retryableerror", nil
}
return nil, "quit", rerr.Err
},
}
if delay.Milliseconds() > 0 {
c.Delay = delay
}
if delayRand.Milliseconds() > 0 {
// Hitting the API at exactly the same time on each iteration of the retry is more likely to
// cause Throttling problems. We introduce randomness in order to help AWS be happier.
rand.Seed(time.Now().UTC().UnixNano())
c.Delay = time.Duration(rand.Int63n(delayRand.Milliseconds())) * time.Millisecond
}
if minTimeout.Milliseconds() > 0 {
c.MinTimeout = minTimeout
}
if pollInterval.Milliseconds() > 0 {
c.PollInterval = pollInterval
}
_, waitErr := c.WaitForStateContext(ctx)
// Need to acquire the lock here to be able to avoid race using resultErr as
// the return value
resultErrMu.Lock()
defer resultErrMu.Unlock()
// resultErr may be nil because the wait timed out and resultErr was never
// set; this is still an error
if resultErr == nil {
return waitErr
}
// resultErr takes precedence over waitErr if both are set because it is
// more likely to be useful
return resultErr
}