module HTTPX::Plugins::Retries

  1. lib/httpx/plugins/retries.rb

This plugin adds support for retrying requests when errors happen.

It has a default max number of retries (see MAX_RETRIES and the max_retries option), after which it will return the last response, error or not. It will not raise an exception.

It does not retry which are not considered idempotent (see retry_change_requests to override).

gitlab.com/os85/httpx/wikis/Retries

Constants

BACKOFF_ALGORITHMS = %i[exponential_backoff polynomial_backoff].freeze  

list of supported backoff algorithms

DEFAULT_JITTER = ->(interval) { interval * ((rand + 1) * 0.5) }.freeze  
IDEMPOTENT_METHODS = %w[GET OPTIONS HEAD PUT DELETE].freeze  

TODO: pass max_retries in a configure/load block

MAX_RETRIES = 3  
RECONNECTABLE_ERRORS = [ IOError, EOFError, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, Errno::EINVAL, Errno::ETIMEDOUT, ConnectionError, TLSError, Zlib::BufError, PingTimeoutError, # HTTP/2 GOAWAY or RST_STREAM errors guaranteed to be retriable for all # types of requests. Connection::HTTP2::RefusedStreamError, Connection::HTTP2::GoawayError, Connection::HTTP2::PingError, # happens if connections were somehow sent to a closed or closing # HTTP2::Connection, request guaranteed to not have been processed. ::HTTP2::Error::ConnectionClosed, ].freeze  

subset of retryable errors which are safe to retry when reconnecting

RETRYABLE_ERRORS = (RECONNECTABLE_ERRORS + [ # most RST_STREAM errors aren't safely retriable, as request may have # been partially processed. Connection::HTTP2::RstStreamError, Parser::Error, TimeoutError, ]).freeze  

Public Class methods

extra_options(options)
[show source]
   # File lib/httpx/plugins/retries.rb
60 def extra_options(options)
61   options.merge(max_retries: MAX_RETRIES)
62 end
retry_after_exponential_backoff(request, _)

returns the time to wait before resending request as per the exponential backoff retry strategy, where base is 2

[show source]
   # File lib/httpx/plugins/retries.rb
78 def retry_after_exponential_backoff(request, _)
79   offset = request.options.max_retries - request.retries
80   2**(offset - 1)
81 end
retry_after_polynomial_backoff(request, _)

returns the time to wait before resending request as per the polynomial backoff retry strategy, where base is 1 and exponent is 2.

[show source]
   # File lib/httpx/plugins/retries.rb
71 def retry_after_polynomial_backoff(request, _)
72   offset = request.options.max_retries - request.retries
73   1 * ((offset - 1)**2)
74 end