Retries¶
Retry
dataclass
¶
Retry(
max_retries: int = 2,
statuses: Collection[int] = frozenset(
{408, 429, 502, 503, 504}
),
methods: Collection[str] = frozenset(
{"GET", "HEAD", "OPTIONS", "PUT", "DELETE"}
),
connection_errors: bool = True,
backoff: float = 0.5,
max_delay: float = 30.0,
jitter: float = 0.25,
respect_retry_after: bool = True,
)
When to re-send a request, and how long to wait first.
Pass one as retry= to a client (or with_options).
Each attempt goes through the client's prepare_request hook again, so signatures
and timestamps are fresh. Only idempotent methods are retried by default: add
"POST" if your API deduplicates with idempotency keys.
Delays grow exponentially (backoff * 2**attempt, capped at max_delay) with
random jitter, unless the response carries a Retry-After header.
Subclass it and override should_retry or
delay for custom rules; the client's
_retry_policy hook picks a policy per route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_retries
|
int
|
Retries after the first attempt (so up to |
2
|
statuses
|
Collection[int]
|
Response status codes that are retried. |
frozenset({408, 429, 502, 503, 504})
|
methods
|
Collection[str]
|
HTTP methods that are retried. |
frozenset({'GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE'})
|
connection_errors
|
bool
|
Whether to retry when no response arrived at all. |
True
|
backoff
|
float
|
Delay before the first retry, in seconds; doubles each attempt. |
0.5
|
max_delay
|
float
|
Upper bound for any single delay, in seconds ( |
30.0
|
jitter
|
float
|
Randomise each delay by up to plus or minus this fraction. |
0.25
|
respect_retry_after
|
bool
|
Use the response's |
True
|
Methods:
| Name | Description |
|---|---|
should_retry |
Decide whether to send the request again. |
delay |
Seconds to wait before the next attempt. |
should_retry
¶
Decide whether to send the request again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attempt
|
int
|
Retries made so far (0 after the first attempt). |
required |
request
|
Request
|
The request that was sent. |
required |
response
|
Response | None
|
The response, or |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
delay
¶
Seconds to wait before the next attempt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attempt
|
int
|
Retries made so far (0 after the first attempt). |
required |
response
|
Response | None
|
The response that triggered the retry, or |
required |
Returns:
| Type | Description |
|---|---|
float
|
The |
float
|
backoff with jitter; never more than |