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

Change the arguments when backoff occurs #41

Closed
Jeffrey04 opened this issue Jul 10, 2017 · 2 comments
Closed

Change the arguments when backoff occurs #41

Jeffrey04 opened this issue Jul 10, 2017 · 2 comments

Comments

@Jeffrey04
Copy link

Jeffrey04 commented Jul 10, 2017

Suppose I have a function that sends a HTTP request with a token that is to be refreshed every N minutes that is not under my control. So I wrap the token fetching request into a separate closure that can be called whenever needed.

def token_fetcher():
    token = None

    def _fetcher(renew):
        nonlocal token
        if token is None or renew:
            token = do_something_to_fetch_the_token()

        return token

    return _fetcher

and my actual function doing HTTP request

def do_http_request(query, token_fetcher, renew=False):
    return requests.get('http://example.com/request', param=query, header=token_fetcher(renew))

So is it possible to make backoff to flip renew into True if backoff happens? (second trial onwards)

@bgreen-litl
Copy link
Member

bgreen-litl commented Jul 10, 2017

Could you do something like this?

def do_http_request(query):
    token = None
    renew = False

    def set_renew(details):
        nonlocal renew
        renew = True

    def token_fetcher():
        nonlocal token
        nonlocal renew
        if token is None or renew:
            token = do_something_to_fetch_the_token()

        return token

    @backoff.on_exception([...], on_backoff=set_renew)
    def _do_http_request():
        return requests.get('http://example.com/request', param=query, header=token_fetcher(renew))

    _do_http_request()

@Jeffrey04
Copy link
Author

oh ok whacks head
thanks (:

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

2 participants