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(opts) ⇒ 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.

Returns a new instance of Performer.

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :tries (#to_i) — default: 5
  • :delay (#call, #to_i) — default: DELAY_PROC
  • :exceptions (Array(Exception)) — default: RETRIABLE_ERRORS
  • :retry_statuses (Array(#to_i))
  • :on_retry (#call)
  • :max_delay (#to_f) — default: Float::MAX
  • :should_retry (#call)


35
36
37
38
39
40
41
42
# File 'lib/http/retriable/performer.rb', line 35

def initialize(opts)
  @exception_classes = opts.fetch(:exceptions, RETRIABLE_ERRORS)
  @retry_statuses = opts[:retry_statuses]
  @tries = opts.fetch(:tries, 5).to_i
  @on_retry = opts.fetch(:on_retry, ->(*) {})
  @should_retry_proc = opts[:should_retry]
  @delay_calculator = DelayCalculator.new(opts)
end

Instance Method Details

#calculate_delay(iteration, response) ⇒ Object

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.



77
78
79
# File 'lib/http/retriable/performer.rb', line 77

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

#perform(client, req, &block) ⇒ Object

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.

Watches request/response execution.

If any of RETRIABLE_ERRORS occur or response status is 5xx, retries up to :tries amount of times. Sleeps for amount of seconds calculated with :delay proc before each retry.

See Also:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/http/retriable/performer.rb', line 52

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)
      begin
        wait_for_retry_or_raise(req, err, res, attempt)
      ensure
        # Some servers support Keep-Alive on any response. Thus we should
        # flush response before retry, to avoid state error (when socket
        # has pending response data and we try to write new request).
        # Alternatively, as we don't need response body here at all, we
        # are going to close client, effectivle closing underlying socket
        # and resetting client's state.
        client.close
      end
    elsif err
      client.close
      raise err
    elsif res
      return res
    end
  end
end