Sha256: c754c0773ae27134e30e76a099ba76bd68d84af0dfb5ee9c7c6ecffd46acba32

Contents?: true

Size: 1.81 KB

Versions: 17

Compression:

Stored size: 1.81 KB

Contents

module Ridley
  # @author Jamie Winsor <reset@riotgames.com>
  #
  # Catches exceptions and retries each request a limited number of times.
  #
  # @example
  #
  #   Faraday.new do |conn|
  #     conn.request :retry, max: 2, interval: 0.05, exceptions: [CustomException, Faraday::Timeout::Error]
  #     conn.adapter ...
  #   end
  #
  # @note Borrowed and modified from: {https://github.com/lostisland/faraday/blob/master/lib/faraday/request/retry.rb}
  #   use the Faraday official middleware after the release of 0.9.x
  class Middleware::Retry < Faraday::Middleware
    # @option options [Integer] :max
    #   maximum number of retries
    # @option options [Float] :interval
    #   pause in seconds between retries
    # @option options [Array] :exceptions
    #   the list of exceptions to handle
    def initialize(app, options = {})
      super(app)
      @options  = options.slice(:max, :interval, :exceptions)
      @errmatch = build_exception_matcher(@options[:exceptions])
    end

    def call(env)
      retries = @options[:max]
      begin
        @app.call(env)
      rescue @errmatch
        if retries > 0
          retries -= 1
          sleep @options[:interval] if @options[:interval] > 0
          retry
        end
        raise
      end
    end

    # 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.
    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
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ridley-0.12.2 lib/ridley/middleware/retry.rb
ridley-0.12.1 lib/ridley/middleware/retry.rb
ridley-0.12.0 lib/ridley/middleware/retry.rb
ridley-0.11.2 lib/ridley/middleware/retry.rb
ridley-0.12.0.rc1 lib/ridley/middleware/retry.rb
ridley-0.11.1 lib/ridley/middleware/retry.rb
ridley-0.10.2 lib/ridley/middleware/retry.rb
ridley-0.9.1 lib/ridley/middleware/retry.rb
ridley-0.10.1 lib/ridley/middleware/retry.rb
ridley-0.11.0.rc1 lib/ridley/middleware/retry.rb
ridley-0.10.0 lib/ridley/middleware/retry.rb
ridley-0.10.0.rc3 lib/ridley/middleware/retry.rb
ridley-0.10.0.rc2 lib/ridley/middleware/retry.rb
ridley-0.10.0.rc1 lib/ridley/middleware/retry.rb
ridley-0.9.0 lib/ridley/middleware/retry.rb
ridley-0.8.6 lib/ridley/middleware/retry.rb
ridley-0.8.5 lib/ridley/middleware/retry.rb