backoff icon indicating copy to clipboard operation
backoff copied to clipboard

How can I change `max_time` by http status code.

Open yuji38kwmt opened this issue 2 years ago • 2 comments

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?

yuji38kwmt avatar Oct 21 '21 07:10 yuji38kwmt

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.

hbrooks avatar Feb 14 '22 13:02 hbrooks

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?

bgreen-litl avatar Apr 26 '22 14:04 bgreen-litl