Class: DeskApi::Request::Retry

Inherits:
Faraday::Request::Retry
  • Object
show all
Defined in:
lib/desk_api/request/retry.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Retry

Returns a new instance of Retry.



4
5
6
7
8
# File 'lib/desk_api/request/retry.rb', line 4

def initialize(app, options = {})
  @max = options[:max] || 3
  @interval = options[:interval] || 10
  super(app)
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/desk_api/request/retry.rb', line 10

def call(env)
  retries   = @retries
  env_clone = env.clone
  begin
    @app.call(env)
  rescue DeskApi::Error::TooManyRequests => e
    if retries > 0 and e.rate_limit.reset_in
      retries = 0
      sleep e.rate_limit.reset_in
      env = env_clone
      retry
    end
    raise
  rescue exception_matcher
    if retries > 0
      retries -= 1
      sleep @interval
      env = env_clone
      retry
    end
    raise
  end
end

#exception_matcherObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/desk_api/request/retry.rb', line 34

def exception_matcher
  exceptions = [Errno::ETIMEDOUT, 'Timeout::Error', Faraday::Error::TimeoutError]
  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