LoggingRetry

Documentation for eth_defi.velvet.logging_retry.LoggingRetry Python class.

class LoggingRetry

Bases: urllib3.util.retry.Retry

In the case we need to throttle Coingecko or other HTTP API, be verbose about it.

Example how to use:

from requests.sessions import HTTPAdapter

# Set up dealing with network connectivity flakey
if retry_policy is None:
    # https://stackoverflow.com/a/35504626/315168
    retry_policy = LoggingRetry(
        total=5,
        backoff_factor=0.1,
        status_forcelist=[429, 500, 502, 503, 504],
    )
    session.mount("http://", HTTPAdapter(max_retries=retry_policy))
    session.mount("https://", HTTPAdapter(max_retries=retry_policy))

Attributes summary

DEFAULT

DEFAULT_ALLOWED_METHODS

Default methods to be used for allowed_methods

DEFAULT_BACKOFF_MAX

Default maximum backoff time.

DEFAULT_REMOVE_HEADERS_ON_REDIRECT

Default headers to be used for remove_headers_on_redirect

RETRY_AFTER_STATUS_CODES

Default status codes to be used for status_forcelist

Methods summary

__init__(*args, **kwargs)

from_int(retries[, redirect, default])

Backwards-compatibility for the old retries format.

get_backoff_time()

Formula for computing the current backoff

get_retry_after(response)

Get the value of Retry-After in seconds.

increment([method, url, response, error, ...])

Return a new Retry object with incremented retry counters.

is_exhausted()

Are we out of retries?

is_retry(method, status_code[, has_retry_after])

Is this method/status code retryable? (Based on allowlists and control variables such as the number of total retries to allow, whether to respect the Retry-After header, whether this header is present, and whether the returned status code is on the list of status codes to be retried upon on the presence of the aforementioned header)

new(**kw)

parse_retry_after(retry_after)

sleep([response])

Sleep between retry attempts.

sleep_for_retry(response)

__init__(*args, **kwargs)
increment(method=None, url=None, response=None, error=None, _pool=None, _stacktrace=None)

Return a new Retry object with incremented retry counters.

Parameters
  • response (BaseHTTPResponse) – A response object, or None, if the server did not return a response.

  • error (Exception) – An error encountered during the request, or None if the response was received successfully.

Returns

A new Retry object.

classmethod from_int(retries, redirect=True, default=None)

Backwards-compatibility for the old retries format.

Parameters
  • retries (urllib3.util.retry.Retry | bool | int | None) –

  • redirect (bool | int | None) –

  • default (urllib3.util.retry.Retry | bool | int | None) –

Return type

urllib3.util.retry.Retry

get_backoff_time()

Formula for computing the current backoff

Return type

float

get_retry_after(response)

Get the value of Retry-After in seconds.

Parameters

response (BaseHTTPResponse) –

Return type

float | None

is_exhausted()

Are we out of retries?

Return type

bool

is_retry(method, status_code, has_retry_after=False)

Is this method/status code retryable? (Based on allowlists and control variables such as the number of total retries to allow, whether to respect the Retry-After header, whether this header is present, and whether the returned status code is on the list of status codes to be retried upon on the presence of the aforementioned header)

Parameters
  • method (str) –

  • status_code (int) –

  • has_retry_after (bool) –

Return type

bool

sleep(response=None)

Sleep between retry attempts.

This method will respect a server’s Retry-After response header and sleep the duration of the time requested. If that is not present, it will use an exponential backoff. By default, the backoff factor is 0 and this method will return immediately.

Parameters

response (BaseHTTPResponse | None) –

Return type

None