API
RetryTransport
Bases: BaseTransport
, AsyncBaseTransport
A transport that automatically retries requests.
with httpx.Client(transport=RetryTransport()) as client:
response = client.get("https://example.com")
async with httpx.AsyncClient(transport=RetryTransport()) as client:
response = await client.get("https://example.com")
If you want to use a specific retry strategy, provide a Retry configuration:
retry = Retry(total=5, backoff_factor=0.5)
transport = RetryTransport(retry=retry)
with httpx.Client(transport=transport) as client:
response = client.get("https://example.com")
By default, the implementation will create a sync and async transport internally, and use whichever is appropriate
for the request. If you want to configure your own transport, provide it to the transport
argument:
transport = RetryTransport(transport=httpx.HTTPTransport(local_address="0.0.0.0"))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transport
|
Optional[Union[HTTPTransport, AsyncHTTPTransport]]
|
Optional transport to wrap. If not provided, async and sync transports are created internally. |
None
|
retry
|
Optional[Retry]
|
The retry configuration. |
None
|
Source code in httpx_retries/transport.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
handle_async_request(request)
async
Sends an HTTP request, possibly with retries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request to perform. |
required |
Returns:
Type | Description |
---|---|
Response
|
The final response. |
Source code in httpx_retries/transport.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
handle_request(request)
Sends an HTTP request, possibly with retries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request to send. |
required |
Returns:
Type | Description |
---|---|
Response
|
The final response. |
Source code in httpx_retries/transport.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
Retry
A class to encapsulate retry logic and configuration.
Each retry attempt will create a new Retry object with updated values, so they can safely be reused.
If backoff_factor
is set, it will use an exponential backoff with configurable jitter.
For complex use cases, you can override the backoff_strategy
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
total
|
int
|
The maximum number of times to retry a request before giving up. |
10
|
max_backoff_wait
|
float
|
The maximum time in seconds to wait between retries. |
120.0
|
backoff_factor
|
float
|
The factor by which the wait time increases with each retry attempt. |
0.0
|
respect_retry_after_header
|
bool
|
Whether to respect the Retry-After header in HTTP responses when deciding how long to wait before retrying. |
True
|
allowed_methods
|
Iterable[HTTPMethod, str]
|
The HTTP methods that can be retried. Defaults to ["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"]. |
None
|
status_forcelist
|
Iterable[HTTPStatus, int]
|
The HTTP status codes that can be retried. Defaults to [429, 502, 503, 504]. |
None
|
backoff_jitter
|
float
|
The amount of jitter to add to the backoff time, between 0 and 1. Defaults to 1 (full jitter). |
1.0
|
attempts_made
|
int
|
The number of attempts already made. |
0
|
Source code in httpx_retries/retry.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
asleep(response)
async
Sleep between retry attempts asynchronously using the calculated duration.
Source code in httpx_retries/retry.py
206 207 208 |
|
backoff_strategy()
Calculate the backoff time based on the number of attempts.
For complex use cases, you can override this method to implement a custom backoff strategy.
class CustomRetry(Retry):
def backoff_strategy(self) -> float:
if self.attempts_made == 3:
return 1.0
return super().backoff_strategy()
Returns:
Type | Description |
---|---|
float
|
The calculated backoff time in seconds, capped by max_backoff_wait. |
Source code in httpx_retries/retry.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
increment()
Return a new Retry instance with the attempt count incremented.
Source code in httpx_retries/retry.py
210 211 212 213 214 215 216 217 218 219 220 221 |
|
is_exhausted()
Check if the retry attempts have been exhausted.
Source code in httpx_retries/retry.py
127 128 129 |
|
is_retry(method, status_code, has_retry_after)
Check if a method and status code are retryable.
This functions identically to urllib3's Retry.is_retry
method.
Source code in httpx_retries/retry.py
114 115 116 117 118 119 120 121 122 123 124 125 |
|
is_retryable_method(method)
Check if a method is retryable.
Source code in httpx_retries/retry.py
106 107 108 |
|
is_retryable_status_code(status_code)
Check if a status code is retryable.
Source code in httpx_retries/retry.py
110 111 112 |
|
parse_retry_after(retry_after)
Parse the Retry-After header.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retry_after
|
str
|
The Retry-After header value. |
required |
Returns:
Type | Description |
---|---|
float
|
The number of seconds to wait before retrying. |
Raises:
Type | Description |
---|---|
ValueError
|
If the Retry-After header is not a valid number or HTTP date. |
Source code in httpx_retries/retry.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
sleep(response)
Sleep between retry attempts using the calculated duration.
Source code in httpx_retries/retry.py
202 203 204 |
|