Class: Faraday::Conductivity::Repeater

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/conductivity/repeater.rb

Defined Under Namespace

Classes: Pattern

Constant Summary collapse

PATTERNS =
{
  :rapid       => lambda { |n| 0 },
  :one         => lambda { |n| 1 },
  :linear      => lambda { |n| n },
  :exponential => lambda { |n| n ** 2 },
}

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Repeater.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/faraday/conductivity/repeater.rb', line 12

def initialize(app, options = {})
  @app = app
  @retries = options[:retries] || 10

  if mode = options[:mode]
    @pattern = build_pattern(PATTERNS.fetch(mode))
  elsif pattern = options[:pattern]
    @pattern = build_pattern(pattern)
  else
    @pattern = build_pattern(PATTERNS.fetch(:exponential))
  end
end

Instance Method Details

#build_pattern(pattern) ⇒ Object



40
41
42
# File 'lib/faraday/conductivity/repeater.rb', line 40

def build_pattern(pattern)
  Pattern.new(pattern)
end

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/faraday/conductivity/repeater.rb', line 25

def call(env)
  tries = 0
  begin
    @app.call(env)
  rescue Faraday::Error::ClientError
    if tries < @retries
      tries += 1
      @pattern.wait(tries)
      retry
    else
      raise
    end
  end
end