Module: HTTPX::Plugins::Retries::InstanceMethods
- Defined in:
- lib/httpx/plugins/retries.rb,
sig/plugins/retries.rbs
Instance Method Summary collapse
-
#can_reconnect?(request, _) ⇒ Boolean
do not exhaust retries on connection liveness probes.
- #fetch_response(request, selector, options) ⇒ retriesResponse?
-
#max_retries(n) ⇒ instance
returns a
:retriesplugin enabled session withnmaximum retries per request setting. - #prepare_to_retry(request, response) ⇒ void
-
#retryable_error?(ex, _) ⇒ Boolean
returns whether the
exexception happend for a retriable request. -
#retryable_request?(request, _, options) ⇒ Boolean
returns whether
requestcan be retried. - #retryable_response?(response, options) ⇒ Boolean
-
#try_partial_retry(request, response) ⇒ void
Attempt to set the request to perform a partial range request.
- #when_to_retry(request, response, options) ⇒ Numeric?
Instance Method Details
#can_reconnect?(request, _) ⇒ Boolean
do not exhaust retries on connection liveness probes
206 207 208 |
# File 'lib/httpx/plugins/retries.rb', line 206 def can_reconnect?(request, _) request.ping? end |
#fetch_response(request, selector, options) ⇒ retriesResponse?
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 |
# File 'lib/httpx/plugins/retries.rb', line 145 def fetch_response(request, selector, ) response = super if response && request.retries.positive? && retryable_request?(request, response, ) && retryable_response?(response, ) try_partial_retry(request, response) log { "failed to get response, #{request.retries} tries to go..." } # retry-after must be calculated before prepare_to_retry, as it relies on # state changed byit. retry_after = when_to_retry(request, response, ) prepare_to_retry(request, response) if retry_after&.positive? retry_start = Utils.now log { "retrying after #{retry_after} secs..." } selector.after(retry_after) do if (response = request.response) response.finish! # request has terminated abruptly meanwhile request.emit_response(response) else log { "retrying (elapsed time: #{Utils.elapsed_time(retry_start)})!!" } send_request(request, selector, ) end end return else send_request(request, selector, ) # recalling itself, in case an error was triggered by the above, and we can # verify retriability again. return fetch_response(request, selector, ) end end response end |
#max_retries(n) ⇒ instance
returns a :retries plugin enabled session with n maximum retries per request setting.
139 140 141 |
# File 'lib/httpx/plugins/retries.rb', line 139 def max_retries(n) with(max_retries: n) end |
#prepare_to_retry(request, response) ⇒ void
This method returns an undefined value.
200 201 202 203 |
# File 'lib/httpx/plugins/retries.rb', line 200 def prepare_to_retry(request, response) request.retries -= 1 unless can_reconnect?(request, response) request.transition(:idle) end |
#retryable_error?(ex, _) ⇒ Boolean
returns whether the ex exception happend for a retriable request.
196 197 198 |
# File 'lib/httpx/plugins/retries.rb', line 196 def retryable_error?(ex, _) RETRYABLE_ERRORS.any? { |klass| ex.is_a?(klass) } && !ex.is_a?(TotalRequestTimeoutError) end |
#retryable_request?(request, _, options) ⇒ Boolean
returns whether request can be retried.
187 188 189 |
# File 'lib/httpx/plugins/retries.rb', line 187 def retryable_request?(request, _, ) IDEMPOTENT_METHODS.include?(request.verb) || .retry_change_requests end |
#retryable_response?(response, options) ⇒ Boolean
191 192 193 |
# File 'lib/httpx/plugins/retries.rb', line 191 def retryable_response?(response, ) (response.is_a?(ErrorResponse) && retryable_error?(response.error, )) || .retry_on&.call(response) end |
#try_partial_retry(request, response) ⇒ void
This method returns an undefined value.
Attempt to set the request to perform a partial range request. This happens if the peer server accepts byte-range requests, and the last response contains some body payload.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/httpx/plugins/retries.rb', line 231 def try_partial_retry(request, response) response = response.response if response.is_a?(ErrorResponse) return unless response unless response.headers.key?("accept-ranges") && response.headers["accept-ranges"] == "bytes" && # there's nothing else supported though... (original_body = response.body) response.body.close return end request.partial_response = response size = original_body.bytesize request.headers["range"] = "bytes=#{size}-" end |
#when_to_retry(request, response, options) ⇒ Numeric?
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/httpx/plugins/retries.rb', line 210 def when_to_retry(request, response, ) retry_after = .retry_after return unless retry_after retry_after = retry_after.call(request, response) if retry_after.respond_to?(:call) return unless retry_after # apply jitter if (jitter = request..retry_jitter) retry_after = jitter.call(retry_after) end retry_after end |