Module: Faraday::Retryable

Included in:
Faraday::Retry::Middleware
Defined in:
lib/faraday/retry/retryable.rb

Overview

Adds the ability to retry a request based on settings and errors that have occurred.

Instance Method Summary collapse

Instance Method Details

#with_retries(env:, options:, retries:, body:, errmatch:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/faraday/retry/retryable.rb', line 6

def with_retries(env:, options:, retries:, body:, errmatch:)
  yield
rescue errmatch => e
  exhausted_retries(options, env, e) if retries_zero?(retries, env, e)

  if retries.positive? && retry_request?(env, e)
    retries -= 1
    rewind_files(body)
    if (sleep_amount = calculate_sleep_amount(retries + 1, env))
      options.retry_block.call(
        env: env,
        options: options,
        retry_count: options.max - (retries + 1),
        exception: e,
        will_retry_in: sleep_amount
      )
      sleep sleep_amount
      retry
    end
  end

  raise unless e.is_a?(Faraday::RetriableResponse)

  e.response
end