question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inspect exception or return value in wait_gen (or wait_gen override)

See original GitHub issue

I wanted to see if there is interest in an API addition to allow users to determine wait time based on exception or return value from the decorated function.

One use case being: You’re sending requests to a rate limited API. API blocks your request but is nice enough to include Retry-After header in the response.
Retry-After contains the exact number of seconds you should wait before making another request.

If I could access the exception or response somehow before wait time is decided, I could check for this header and if present use its value, otherwise fall back on a wait_gen
Using any kind of calculated time function is very hit and miss in this scenario and mostly wasteful.

I’m thinking about adding an optional wait_override argument to the decorators. This would be a function that gets passed the exception (for backoff.on_exception) or the retry-able’s return value (for backoff.on_predicate).
So that users can do something like this:

def my_override(exception):
    headers = getattr(e, 'headers', {})
    seconds = headers.get('Retry-After')
    # If None is returned, wait_gen takes over
    return int(seconds) if seconds else None
    
@backoff.on_exception(backoff.expo, HTTPError, wait_override=my_override)    
def request_thing(url):
    response = do_request(...)    
    response.raise_for_status()
    return response

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bgreen-litlcommented, May 5, 2022

@edgarrmondragon Yes. Thanks. And released in latest backoff (2.0.1)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Are there reasons for returning exception objects instead of ...
I like to think of throwing an exception as a "super return" statement for a function. It returns execution of the code to...
Read more >
Exception Handling with Method Overriding in Java
When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in...
Read more >
database - Liquibase lock - reasons? - Stack Overflow
I get this when running a lot of liquibase-scripts against a Oracle-server. SomeComputer is me. Waiting for changelog lock.... Waiting for changelog lock.......
Read more >
Troubleshoot connector errors for Amazon MSK Connect
TimeoutException : Timed out waiting to send the call. ... You can use the text editor to check and remove the carriage return...
Read more >
The transaction block doesn't check the connection before ...
I think the current behaviour is fine and appropriate. It is an error to close the connection in the midst of a transaction...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found