Class: Faraday::BetterRetry
- Inherits:
-
Middleware
- Object
- Middleware
- Faraday::BetterRetry
- Defined in:
- lib/faraday/better_retry.rb
Overview
Catches exceptions and retries each request a limited number of times.
By default, it retries 2 times and handles only timeout exceptions. It can be configured with an arbitrary number of retries, a list of exceptions to handle an a retry interval.
Examples
Faraday.new do |conn|
conn.request :retry, max: 2, interval: 0.05,
exceptions: [CustomException, 'Timeout::Error']
conn.adapter ...
end
Defined Under Namespace
Classes: Options
Instance Method Summary collapse
-
#build_exception_matcher(exceptions) ⇒ Object
Private: construct an exception matcher object.
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ BetterRetry
constructor
Public: Initialize middleware.
Constructor Details
#initialize(app, options = {}) ⇒ BetterRetry
Public: Initialize middleware
Options: max - Maximum number of retries (default: 2). interval - Pause in seconds between retries (default: 0). exceptions - The list of exceptions to handle. Exceptions can be
given as Class, Module, or String. (default:
[Errno::ETIMEDOUT, Timeout::Error, Error::TimeoutError])
51 52 53 54 55 56 |
# File 'lib/faraday/better_retry.rb', line 51 def initialize(app, = {}) super(app) @options = Options.new() @errmatch = build_exception_matcher(@options.exceptions) @logger = [:logger] end |
Instance Method Details
#build_exception_matcher(exceptions) ⇒ Object
Private: construct an exception matcher object.
An exception matcher for the rescue clause can usually be any object that responds to ‘===`, but for Ruby 1.8 it has to be a Class or Module.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/faraday/better_retry.rb', line 80 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 then error.is_a? ex else error.class.to_s == ex.to_s end end end end matcher end |
#call(env) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/faraday/better_retry.rb', line 58 def call(env) env[:retries] = retries = @options.max begin @app.call(env) rescue @errmatch => e if retries > 0 if @logger @logger.warn("Retrying Parse Error #{e.inspect} on request #{env[:url].to_s} #{env[:body].inspect} response #{env[:response].inspect}") end retries -= 1 env[:retries] = retries sleep @options.interval if @options.interval > 0 retry end raise end end |