backoff
backoff copied to clipboard
How can I change `max_time` by http status code.
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?
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: https://github.com/litl/backoff/blob/c2f4e1826b1996348177215f4c009c055777603f/backoff/_sync.py#L35
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.
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?