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

How can I change max_time by http status code. #143

Open
yuji38kwmt opened this issue Oct 21, 2021 · 2 comments
Open

How can I change max_time by http status code. #143

yuji38kwmt opened this issue Oct 21, 2021 · 2 comments

Comments

@yuji38kwmt
Copy link

yuji38kwmt commented Oct 21, 2021

I am retrying web api request when http status code is 5XX.

def fatal_code(e):
    return 400 <= e.response.status_code < 500

@backoff.on_exception(backoff.expo,
                      requests.exceptions.RequestException,
                      max_time=300,
                      giveup=fatal_code)
def get_url(url):
    return requests.get(url)

Further, I wolud like to change max_time by http status code.
For example ...

if 400 <= status_code < 500:
    max_time = 60
else:
    max_time = 300

How can I do?

@hbrooks
Copy link

hbrooks commented Feb 14, 2022

Looks like you'd need to pass a callable to max_time and we'd need to pass the exception to it in this line:

max_time = _maybe_call(max_time)

At the moment this doesn't appear possible - but heads up, I'm not a contributor, just a user of the library so I'm not as familiar with this code as others. I'm sure the authors would appreciate an issue being opened, if a similar one doesn't already exist or has been discussed.

@bgreen-litl
Copy link
Member

I have thought about adding the details dict as a parameter to some of the other "maybe callables"... However for max_time in particular, I think would be suspect because the max time being measured is for all retries. Referencing your example above, what happens if one time the status_code is 403 and the next it is 503?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants