Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: 429 with exponential backoff fallback example #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,14 @@ gets a non-falsey result could be defined like like this:
def poll_for_message(queue):
return queue.get()

The jitter is disabled in order to keep the polling frequency fixed.
The jitter is disabled in order to keep the polling frequency fixed.

@backoff.runtime
----------------

You can also use the ``backoff.runtime`` generator to make use of the
return value or thrown exception of the decorated method.
return value (in the case of `on_predicate`) or thrown exception (in the case of `on_exception`)
of the decorated method.

For example, to use the value in the ``Retry-After`` header of the response:

Expand All @@ -191,6 +192,37 @@ For example, to use the value in the ``Retry-After`` header of the response:
def get_url():
return requests.get(url)

Or, if you are using `raise_for_status` you can setup a custom handler for 429s and use `backoff.expo` for everythig else:

.. code-block:: python

def extract_retry_time(exception):
if (
not isinstance(exception, requests.exceptions.HTTPError)
or exception.response.status_code != 429
):
# re-raise the exception if it's not a 429 so it's processed by the `backoff.expo` decorator
raise exception

return int(exception.response.headers.get("Retry-After"))

@backoff.on_exception(
backoff.expo,
(requests.exceptions.HTTPError),
max_tries=8,
)
@backoff.on_exception(
backoff.runtime,
(requests.exceptions.HTTPError),
value=extract_retry_time
)
def get_url(url):
response = requests.get(url)
response.raise_for_status()

json_response = response.json()
return json_response

Jitter
------

Expand Down