-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
retry_errors.rb
171 lines (138 loc) · 4.76 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
require 'set'
module Aws
module Plugins
# @api private
class RetryErrors < Seahorse::Client::Plugin
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_backoff, lambda { |c| Kernel.sleep(2 ** c.retries * 0.3) })
# @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
'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))
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
private
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)
retryable?(context, error) and
context.retries < retry_limit(context) and
(response_truncatable?(context) or !error.invalid_bytes?)
end
def retryable?(context, error)
(error.expired_credentials? and refreshable_credentials?(context)) or
error.throttling_error? or
error.checksum? or
error.networking? or
error.server?
end
def refreshable_credentials?(context)
context.config.credentials.respond_to?(:refresh!)
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