Sha256: f56e2d3cea2334acabec7d10309d98c40a6d4df90d308925112158e5f71f8952

Contents?: true

Size: 1.64 KB

Versions: 13

Compression:

Stored size: 1.64 KB

Contents

require 'faraday'



module Saddle
  module Middleware
    module Request

      # Catches exceptions and retries each request a limited number of times.
      #
      # By default, it retries 0 times and performs exponential backoff, starting
      # at 50ms
      class Retry < Faraday::Middleware
        def initialize(app, ignored_exceptions=[])
          super(app)
          @ignored_exceptions = ignored_exceptions
        end

        def call(env)
          retries = env[:request][:num_retries] || 0
          backoff = env[:request][:retry_backoff] || 0.050 # in seconds
          begin
            @app.call(self.class.deep_copy(env))
          rescue => e
            # Only retry for GET or if the request is marked as idempotent
            if env[:method] == :get || env[:request][:idempotent]
              unless @ignored_exceptions.include?(e.class)
                # Retry a limited number of times
                if retries > 0
                  retries -= 1
                  sleep(backoff) if backoff > 0.0
                  backoff *= 2
                  retry
                end
              end
            end
            # Re-raise if we're out of retries or it's not handled
            raise
          end
        end

        def self.deep_copy(value)
          if value.is_a?(Hash)
            result = value.clone
            value.each{|k, v| result[k] = deep_copy(v)}
            result
          elsif value.is_a?(Array)
            result = value.clone
            result.clear
            value.each{|v| result << deep_copy(v)}
            result
          else
            value
          end
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
saddle-0.0.53 lib/saddle/middleware/request/retry.rb
saddle-0.0.51 lib/saddle/middleware/request/retry.rb
saddle-0.0.50 lib/saddle/middleware/request/retry.rb
saddle-0.0.49 lib/saddle/middleware/request/retry.rb
saddle-0.0.48 lib/saddle/middleware/request/retry.rb
saddle-0.0.46 lib/saddle/middleware/request/retry.rb
saddle-0.0.45 lib/saddle/middleware/request/retry.rb
saddle-0.0.44 lib/saddle/middleware/request/retry.rb
saddle-0.0.43 lib/saddle/middleware/request/retry.rb
saddle-0.0.42 lib/saddle/middleware/request/retry.rb
saddle-0.0.41 lib/saddle/middleware/request/retry.rb
saddle-0.0.38 lib/saddle/middleware/request/retry.rb
saddle-0.0.37 lib/saddle/middleware/request/retry.rb