Class: HTTP::Retriable::Performer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/http/retriable/performer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Request performing watchdog.

Constant Summary collapse

RETRIABLE_ERRORS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Exceptions we should retry

[
  HTTP::TimeoutError,
  HTTP::ConnectionError,
  IO::EAGAINWaitReadable,
  Errno::ECONNRESET,
  Errno::ECONNREFUSED,
  Errno::EHOSTUNREACH,
  OpenSSL::SSL::SSLError,
  EOFError,
  IOError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(tries: 5, delay: nil, exceptions: RETRIABLE_ERRORS, retry_statuses: nil, on_retry: ->(*_args) {}, max_delay: Float::MAX, should_retry: nil) ⇒ HTTP::Retriable::Performer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new retry performer

Parameters:

  • tries (#to_i) (defaults to: 5)

    maximum number of attempts

  • delay (#call, #to_i, nil) (defaults to: nil)

    delay between retries

  • exceptions (Array<Exception>) (defaults to: RETRIABLE_ERRORS)

    exception classes to retry

  • retry_statuses (Array<#to_i>, nil) (defaults to: nil)

    status codes to retry

  • on_retry (#call) (defaults to: ->(*_args) {})

    callback invoked on each retry

  • max_delay (#to_f) (defaults to: Float::MAX)

    maximum delay between retries

  • should_retry (#call, nil) (defaults to: nil)

    custom retry predicate



38
39
40
41
42
43
44
45
46
# File 'lib/http/retriable/performer.rb', line 38

def initialize(tries: 5, delay: nil, exceptions: RETRIABLE_ERRORS, retry_statuses: nil,
               on_retry: ->(*_args) {}, max_delay: Float::MAX, should_retry: nil)
  @exception_classes = exceptions
  @retry_statuses = retry_statuses
  @tries = tries.to_i
  @on_retry = on_retry
  @should_retry_proc = should_retry
  @delay_calculator = DelayCalculator.new(delay: delay, max_delay: max_delay)
end

Instance Method Details

#calculate_delay(iteration, response) ⇒ Numeric

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculates delay between retries

Parameters:

Returns:

  • (Numeric)


73
74
75
# File 'lib/http/retriable/performer.rb', line 73

def calculate_delay(iteration, response)
  @delay_calculator.call(iteration, response)
end

#perform(client, req, &block) ⇒ HTTP::Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute request with retry logic

Returns:

See Also:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/http/retriable/performer.rb', line 53

def perform(client, req, &block)
  1.upto(Float::INFINITY) do |attempt| # infinite loop with index
    err, res = try_request(&block)

    if retry_request?(req, err, res, attempt)
      retry_attempt(client, req, err, res, attempt)
    elsif err
      finish_attempt(client, err)
    elsif res
      return res
    end
  end
end