class DeskApi::Request::Retry
{DeskApi::Request::Retry} is a Faraday middleware that retries failed requests up to 3 times. It also includes desk.com's rate limiting which are retried only once.
@author Thomas Stachl <tstachl@salesforce.com> @copyright Copyright © 2013-2016 Salesforce.com @license BSD 3-Clause License
Public Class Methods
Returns an array of errors that should be retried.
@return [Array]
# File lib/desk_api/request/retry.rb, line 43 def errors @exceptions ||= [ Errno::ETIMEDOUT, 'Timeout::Error', Faraday::Error::TimeoutError, DeskApi::Error::TooManyRequests, DeskApi::Error::BadRequest ] end
Initializies the middleware and sets options
@param app [Hash] the faraday environment hash @param options [Hash] additional options
# File lib/desk_api/request/retry.rb, line 58 def initialize(app, options = {}) @max = options[:max] || 3 @interval = options[:interval] || 10 super(app) end
Public Instance Methods
Calculates the retries based on the error
@param err [StandardError] the error that has been thrown @param retries [Integer] current retry count @return [Integer]
# File lib/desk_api/request/retry.rb, line 85 def calc(err, retries, &block) # retry only once if err.kind_of?(DeskApi::Error::TooManyRequests) && retries == @max - 1 block.call(0) else block.call(retries - 1) end end
Rescues exceptions and retries the request
@param env [Hash] the request hash
# File lib/desk_api/request/retry.rb, line 67 def call(env) retries = @max request_body = env[:body] begin env[:body] = request_body @app.call(env) rescue exception_matcher => err raise unless calc(err, retries) { |x| retries = x } > 0 sleep interval(err) retry end end
Returns an exception matcher
@return [Module]
# File lib/desk_api/request/retry.rb, line 109 def exception_matcher matcher = Module.new (class << matcher; self; end).class_eval do define_method(:===) do |error| Retry.errors.any? do |ex| ex.is_a?(Module) ? error.is_a?(ex) : error.class.to_s == ex.to_s end end end matcher end
Returns the interval for the specific error
@param err [StandardError] the error that has been thrown @return [Integer]
# File lib/desk_api/request/retry.rb, line 98 def interval(err) if err.kind_of?(DeskApi::Error::TooManyRequests) err.rate_limit.reset_in else @interval end end