class Faraday::Request::Retry
interval that is random between 0.1 and 0.15
This example will result in a first interval that is random between 0.05 and 0.075 and a second
end
conn.adapter …
exceptions: [CustomException, ‘Timeout::Error’]
interval_randomness: 0.5, backoff_factor: 2,
conn.request :retry, max: 2, interval: 0.05,
Faraday.new do |conn|
Examples
interval, and a backoff factor.
handle, a retry interval, a percentage of randomness to add to the retry
be configured with an arbitrary number of retries, a list of exceptions to
By default, it retries 2 times and handles only timeout exceptions. It can
Catches exceptions and retries each request a limited number of times.
def build_exception_matcher(exceptions)
An exception matcher for the rescue clause can usually be any object that
Private: construct an exception matcher object.
def build_exception_matcher(exceptions) matcher = Module.new (class << matcher; self; end).class_eval do define_method(:===) do |error| exceptions.any? do |ex| if ex.is_a? Module error.is_a? ex else error.class.to_s == ex.to_s end end end end matcher end
def call(env)
def call(env) retries = @options.max request_body = env[:body] begin env[:body] = request_body # after failure env[:body] is set to the response body @app.call(env) rescue @errmatch => exception if retries > 0 && retry_request?(env, exception) retries -= 1 rewind_files(request_body) sleep sleep_amount(retries + 1) retry end raise end end
def initialize(app, options = nil)
the HTTP method called is not idempotent.
if the exception produced is non-recoverable or if the
not independent of the retry count. This would be useful
and should decide if the code should retry still the action or
retry_if - block that will receive the env object and the exception raised
(defaults to the idempotent HTTP methods in IDEMPOTENT_METHODS)
an empty Array to call retry_if for all exceptions.
methods - A list of HTTP methods to retry without calling retry_if. Pass
Error::TimeoutError])
[Errno::ETIMEDOUT, Timeout::Error,
given as Class, Module, or String. (default:
exceptions - The list of exceptions to handle. Exceptions can be
(default: 1)
interval amount by in order to provide backoff
backoff_factor - The amount to multiple each successive retry's
max_interval - An upper limit for the interval (default: Float::MAX)
interval. (default: 0)
as a float between 0 and 1 to use in addition to the
interval_randomness - The maximum random interval amount expressed
interval - Pause in seconds between retries (default: 0)
max - Maximum number of retries (default: 2)
Options:
Public: Initialize middleware
def initialize(app, options = nil) super(app) @options = Options.from(options) @errmatch = build_exception_matcher(@options.exceptions) end
def retry_request?(env, exception)
def retry_request?(env, exception) @options.methods.include?(env[:method]) || @options.retry_if.call(env, exception) end
def rewind_files(body)
def rewind_files(body) return unless body.is_a?(Hash) body.each do |_, value| if value.is_a? UploadIO value.rewind end end end
def sleep_amount(retries)
def sleep_amount(retries) retry_index = @options.max - retries current_interval = @options.interval * (@options.backoff_factor ** retry_index) current_interval = [current_interval, @options.max_interval].min random_interval = rand * @options.interval_randomness.to_f * @options.interval current_interval + random_interval end