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

Behavior change when reading max_tries and max_time from callables #160

Closed
jvrsantacruz opened this issue Jun 7, 2022 · 2 comments
Closed

Comments

@jvrsantacruz
Copy link
Contributor

Hi there!

Using a callable to read max_tries and max_time was really useful to query an application config object.
Every time the function was called, the value would be read again and so it remained configurable.

After updating to version 2+, we realized that the max_tries and max_time are read from the callable once and then cached for every other usage of the function. We saw this when some unit tests that were testing that the application reloads the config correctly were broken.

This was really an use case for us and we're pinned to the previous version we had as we cannot recreate every other object that has a decorated function on every config reload and so we'd consider this a regression on this nice functionality :)

-backoff==1.10.0
+backoff==2.0.1

This minimal example reproduces the problem, the output shows the difference when running in those two versions:

import backoff


CONFIG = {'max_tries': 1}


def _from_config():
    print('Reading max_tries: ', CONFIG)
    return CONFIG['max_tries']


@backoff.on_exception(
    backoff.expo,
    Exception,
    max_tries=_from_config,
    on_giveup=lambda details: print('Total tries', details['tries']),
)
def function():
    print('Calling function')
    raise Exception(f'boom!')


print('First attempt', CONFIG)
try:
    function()
except:
    pass

print('----')
CONFIG['max_tries'] = 2  # Config changed

print('Second attempt', CONFIG)
try:
    function()
except:
    pass

The output for 1.10.0 shows that the configuration is read again on every function call:

First attempt {'max_tries': 1}
Reading max_tries:  {'max_tries': 1}
Calling function
Total tries 1
----
Second attempt {'max_tries': 2}
Reading max_tries:  {'max_tries': 2}
Calling function
Calling function
Total tries 2

While on 2.0.1 this is only made once and then cached for every other execution:

First attempt {'max_tries': 1}
Reading max_tries:  {'max_tries': 1}
Calling function
Total tries 1
----
Second attempt {'max_tries': 2}
Calling function
Total tries 1

As you can see, the second time the function already cached the max_tries and does not read it again.

@bgreen-litl
Copy link
Member

This is fixed in master and I'll plan to release as backoff 2.1.0

@bgreen-litl
Copy link
Member

@jvrsantacruz backoff 2.1.0 is released!

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