-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
retry_errors.rb
238 lines (194 loc) · 7 KB
/
retry_errors.rb
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
require 'set'
module Aws
module Plugins
# @api private
class RetryErrors < Seahorse::Client::Plugin
EQUAL_JITTER = lambda { |delay| (delay / 2) + Kernel.rand(0..(delay/2))}
FULL_JITTER= lambda { |delay| Kernel.rand(0..delay) }
NO_JITTER = lambda { |delay| delay }
JITTERS = {
none: NO_JITTER,
equal: EQUAL_JITTER,
full: FULL_JITTER
}
JITTERS.default_proc = lambda { |h,k|
raise KeyError, "#{k} is not a named jitter function. Must be one of #{h.keys}"
}
DEFAULT_BACKOFF = lambda do |c|
delay = 2 ** c.retries * c.config.retry_base_delay
delay = [delay, c.config.retry_max_delay].min if (c.config.retry_max_delay || 0) > 0
jitter = c.config.retry_jitter
jitter = JITTERS[jitter] if Symbol === jitter
delay = jitter.call(delay) if jitter
Kernel.sleep(delay)
end
option(:retry_limit,
default: 3,
doc_type: Integer,
docstring: <<-DOCS)
The maximum number of times to retry failed requests. Only
~ 500 level server errors and certain ~ 400 level client errors
are retried. Generally, these are throttling errors, data
checksum errors, networking errors, timeout errors and auth
errors from expired credentials.
DOCS
option(:retry_max_delay,
default: 0,
doc_type: Integer,
docstring: <<-DOCS)
The maximum number of seconds to delay between retries (0 for no limit) used by the default backoff function.
DOCS
option(:retry_base_delay,
default: 0.3,
doc_type: Float,
docstring: <<-DOCS)
The base delay in seconds used by the default backoff function.
DOCS
option(:retry_jitter,
default: :none,
doc_type: Symbol,
docstring: <<-DOCS)
A delay randomiser function used by the default backoff function. Some predefined functions can be referenced by name - :none, :equal, :full, otherwise a Proc that takes and returns a number.
@see https://www.awsarchitectureblog.com/2015/03/backoff.html
DOCS
option(:retry_backoff, DEFAULT_BACKOFF)
# @api private
class ErrorInspector
EXPIRED_CREDS = Set.new([
'InvalidClientTokenId', # query services
'UnrecognizedClientException', # json services
'InvalidAccessKeyId', # s3
'AuthFailure', # ec2
])
THROTTLING_ERRORS = Set.new([
'Throttling', # query services
'ThrottlingException', # json services
'RequestThrottled', # sqs
'ProvisionedThroughputExceededException', # dynamodb
'TransactionInProgressException', # dynamodb
'RequestLimitExceeded', # ec2
'BandwidthLimitExceeded', # cloud search
'LimitExceededException', # kinesis
'TooManyRequestsException', # batch
])
CHECKSUM_ERRORS = Set.new([
'CRC32CheckFailed', # dynamodb
])
NETWORKING_ERRORS = Set.new([
'RequestTimeout', # s3
])
def initialize(error, http_status_code)
@error = error
@name = extract_name(error)
@http_status_code = http_status_code
end
def expired_credentials?
!!(EXPIRED_CREDS.include?(@name) || @name.match(/expired/i))
end
def throttling_error?
!!(THROTTLING_ERRORS.include?(@name) || @name.match(/throttl/i) || @http_status_code == 429)
end
def checksum?
CHECKSUM_ERRORS.include?(@name) || @error.is_a?(Errors::ChecksumError)
end
def networking?
@error.is_a?(Seahorse::Client::NetworkingError) ||
NETWORKING_ERRORS.include?(@name)
end
def invalid_bytes?
@error.is_a?(Seahorse::Client::NetworkingError) &&
@error.original_error.is_a?(Seahorse::Client::NetHttp::Handler::TruncatedBodyError)
end
def server?
(500..599).include?(@http_status_code)
end
def endpoint_discovery?(context)
return false unless context.operation.endpoint_discovery
if @http_status_code == 421 ||
extract_name(@error) == 'InvalidEndpointException'
@error = Errors::EndpointDiscoveryError.new
end
# When endpoint discovery error occurs
# evict the endpoint from cache
if @error.is_a?(Errors::EndpointDiscoveryError)
key = context.config.endpoint_cache.extract_key(context)
context.config.endpoint_cache.delete(key)
true
else
false
end
end
def retryable?(context)
(expired_credentials? and refreshable_credentials?(context)) or
throttling_error? or
checksum? or
networking? or
server? or
endpoint_discovery?(context)
end
private
def refreshable_credentials?(context)
context.config.credentials.respond_to?(:refresh!)
end
def extract_name(error)
if error.is_a?(Errors::ServiceError)
error.class.code
else
error.class.name.to_s
end
end
end
class Handler < Seahorse::Client::Handler
def call(context)
response = @handler.call(context)
if response.error
retry_if_possible(response)
else
response
end
end
private
def retry_if_possible(response)
context = response.context
error = error_for(response)
if should_retry?(context, error)
retry_request(context, error)
else
response
end
end
def error_for(response)
status_code = response.context.http_response.status_code
ErrorInspector.new(response.error, status_code)
end
def retry_request(context, error)
delay_retry(context)
context.retries += 1
context.config.credentials.refresh! if error.expired_credentials?
context.http_request.body.rewind
context.http_response.reset(truncate: error.invalid_bytes?)
call(context)
end
def delay_retry(context)
context.config.retry_backoff.call(context)
end
def should_retry?(context, error)
error.retryable?(context) and
context.retries < retry_limit(context) and
(response_truncatable?(context) or !error.invalid_bytes?)
end
def retry_limit(context)
context.config.retry_limit
end
def response_truncatable?(context)
context.http_response.body.respond_to?(:truncate)
end
end
def add_handlers(handlers, config)
if config.retry_limit > 0
handlers.add(Handler, step: :sign, priority: 99)
end
end
end
end
end