Skip to content

Commit 67758eb

Browse files
authored
Add retry strategy (#211)
Add retry strategy to python-asana client library Implements automatic retry logic for failed API requests with exponential backoff. By default, requests are retried up to 5 times with increasing delays (1s, 2s, 4s, 8s, 16s). Retries occur on status codes 429, 500, 502, 503, and 504. The retry configuration can be customized by overriding the `retry_strategy` in the configuration object. The implementation respects the `Retry-After` header for rate limit (429) responses.
2 parents 1791774 + 2b09578 commit 67758eb

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

codegen/templates/README.mustache

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,45 @@ api_client = {{{packageName}}}.ApiClient(configuration)
476476
...
477477
```
478478

479+
## Retries
480+
481+
### Default configuration
482+
483+
By default, we retry failed requests (i.e., requests that receive any of the following response statuses: `429`, `500`, `502`, `503`, or `504`)
484+
up to 5 times, using a backoff factor of 2, resulting in wait times of 1s, 2s, 4s, 8s, and 16s between attempts.
485+
For `429 (Too Many Requests)` responses, the `respect_retry_after_header` option is set to `True` by default
486+
(See [urllib3](https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html)), ensuring that retries adhere to the `Retry-After` header
487+
from the [Asana API](https://developers.asana.com/docs/rate-limits).
488+
489+
490+
NOTE: the retry strategy applies to `DELETE`, `GET`, `HEAD`, `OPTIONS`, `PUT`, `TRACE` requests (See `allowed_methods` in [urllib3](https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html))
491+
492+
```
493+
self.retry_strategy = Retry(
494+
total=5, # Number of retries
495+
backoff_factor=2, # Exponential backoff factor (1s, 2s, 4s, etc.)
496+
status_forcelist=[429, 500, 502, 503, 504], # Retry only on these status codes
497+
)
498+
```
499+
500+
### Customize retry configuration/strategy
501+
502+
To customize your retry strategy, you can override the default `retry_strategy` in your configuration.
503+
For details on configurable options for Retry, refer to the documentation: [urllib3](https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html).
504+
505+
#### Example - override default `retry_strategy`
506+
```
507+
import asana
508+
from urllib3.util.retry import Retry
509+
510+
configuration = asana.Configuration()
511+
configuration.retry_strategy = Retry(
512+
total=10, # Maximum number of retries
513+
backoff_factor=4, # Exponential backoff factor (1s, 2s, 4s, etc.)
514+
)
515+
...
516+
```
517+
479518
## Documentation for Using the `call_api` method
480519

481520
Use this to make HTTP calls when the endpoint does not exist in the current library version or has bugs

codegen/templates/configuration.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import logging
99
import multiprocessing
1010
import sys
1111
import urllib3
12+
from urllib3.util.retry import Retry
1213

1314
import six
1415
from six.moves import http_client as httplib
@@ -103,6 +104,13 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
103104
# The default limit query parameter value for api endpoints that return multiple resources
104105
self.page_limit = 100
105106

107+
# Retry Settings
108+
self.retry_strategy = Retry(
109+
total=5, # Number of retries
110+
backoff_factor=2, # Exponential backoff factor (1s, 2s, 4s, etc.)
111+
status_forcelist=[429, 500, 502, 503, 504], # Retry only on these status codes
112+
)
113+
106114
@property
107115
def logger_file(self):
108116
"""The logger file.

codegen/templates/rest.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class RESTClientObject(object):
8383
cert_file=configuration.cert_file,
8484
key_file=configuration.key_file,
8585
proxy_url=configuration.proxy,
86+
retries=configuration.retry_strategy,
8687
**addition_pool_args
8788
)
8889
else:
@@ -93,6 +94,7 @@ class RESTClientObject(object):
9394
ca_certs=ca_certs,
9495
cert_file=configuration.cert_file,
9596
key_file=configuration.key_file,
97+
retries=configuration.retry_strategy,
9698
**addition_pool_args
9799
)
98100

0 commit comments

Comments
 (0)