Retries

Note: Introduced in 0.1.0.

The retries plugin allows requests to be retried when certain errors happen.

:max_retries

You can set how many times a request can be retried by passing the max_retries option (or method), which by default is 3 times:

session = HTTPX.plugin(:retries)

session.max_retries(10).get(URL)
# or
session.get(URL, max_retries: 10)

:retry_after

In case you need to wait before retrying again, you can use the retry_after option, which receives the number of seconds after which you retry, or alternatively, a callable which receives the last failed request and returns the number of seconds to wait (in handy if you want to do exponential backoff):

session = HTTPX.plugin(:retries)

session.max_retries(10).get(URL)
# or
session.get(URL, max_retries: 10)

:retry_on

In order for you to decide on when to retry, you can use the retry_on option, a callable which receives the failed response, to determine whether the request should be retried.

session = HTTPX.plugin(:retries)

session.max_retries(10).get(URL)
# or
session.get(URL, max_retries: 10)

:retry_change_requests

By default, only idempotent requests are retried (GET, PUT…). If you also want, p. ex. POST requests to be retried on, you can pass this option:

session = HTTPX.plugin(:retries)
session.post(URL, body: "body", retry_on: ->(res) { res.error.is_a?(TimeoutError)} ) # only retries if it times out...

Request

The request object has a method, #retries, which returns the number of times the request has been retried.

Errors

Requests will be retried only when connection type errors happen, like network errors (IOError…), TLS errors (OpenSSL::SSL::SSLError…) or timeout errors. A 500 HTTP response will, therefore, be considered a valid response.

Next: Cookies