lib/httpx/plugins/retries.rb in httpx-1.3.0 vs lib/httpx/plugins/retries.rb in httpx-1.3.1
- old
+ new
@@ -1,12 +1,17 @@
# frozen_string_literal: true
module HTTPX
module Plugins
#
- # This plugin adds support for retrying requests when certain errors happen.
+ # This plugin adds support for retrying requests when errors happen.
#
+ # It has a default max number of retries (see *MAX_RETRIES* and the *max_retries* option),
+ # after which it will return the last response, error or not. It will **not** raise an exception.
+ #
+ # It does not retry which are not considered idempotent (see *retry_change_requests* to override).
+ #
# https://gitlab.com/os85/httpx/wikis/Retries
#
module Retries
MAX_RETRIES = 3
# TODO: pass max_retries in a configure/load block
@@ -36,10 +41,18 @@
def self.extra_options(options)
options.merge(max_retries: MAX_RETRIES, retry_jitter: DEFAULT_JITTER)
end
end
+ # adds support for the following options:
+ #
+ # :max_retries :: max number of times a request will be retried (defaults to <tt>3</tt>).
+ # :retry_change_requests :: whether idempotent requests are retried (defaults to <tt>false</tt>).
+ # :retry_after:: seconds after which a request is retried; can also be a callable object (i.e. <tt>->(req, res) { ... } </tt>)
+ # :retry_jitter :: number of seconds applied to *:retry_after* (must be a callable, i.e. <tt>->(retry_after) { ... } </tt>).
+ # :retry_on :: callable which alternatively defines a different rule for when a response is to be retried
+ # (i.e. <tt>->(res) { ... }</tt>).
module OptionsMethods
def option_retry_after(value)
# return early if callable
unless value.respond_to?(:call)
value = Float(value)
@@ -74,11 +87,11 @@
end
end
module InstanceMethods
def max_retries(n)
- with(max_retries: n.to_i)
+ with(max_retries: n)
end
private
def fetch_response(request, connections, options)
@@ -109,12 +122,20 @@
retry_after = jitter.call(retry_after)
end
retry_start = Utils.now
log { "retrying after #{retry_after} secs..." }
+
+ deactivate_connection(request, connections, options)
+
pool.after(retry_after) do
- log { "retrying (elapsed time: #{Utils.elapsed_time(retry_start)})!!" }
- send_request(request, connections, options)
+ if request.response
+ # request has terminated abruptly meanwhile
+ request.emit(:response, request.response)
+ else
+ log { "retrying (elapsed time: #{Utils.elapsed_time(retry_start)})!!" }
+ send_request(request, connections, options)
+ end
end
else
send_request(request, connections, options)
end