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

add common factor for decay #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add common factor for decay #19

wants to merge 1 commit into from

Conversation

mapix
Copy link

@mapix mapix commented Oct 15, 2016

@bgreen-litl pls review

  • Add factor decorator to control sleep time unit.
>>> x = backoff.on_exception(backoff.fibo, Exception, max_tries=4, jitter=None)(t)
>>> x()
Backing off t() 1.0s (Exception: a)
Backing off t() 1.0s (Exception: a)
Backing off t() 2.0s (Exception: a)
Giving up t() after 4 tries (Exception: a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vagrant/backoff/backoff.py", line 302, in retry
    ret = target(*args, **kwargs)
  File "<stdin>", line 2, in t
Exception: a
>>> x = backoff.on_exception(backoff.factor(backoff.fibo, 0.1), Exception, max_tries=4, jitter=None)(t)
>>> x()
Backing off t() 0.1s (Exception: a)
Backing off t() 0.1s (Exception: a)
Backing off t() 0.2s (Exception: a)
Giving up t() after 4 tries (Exception: a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vagrant/backoff/backoff.py", line 302, in retry
    ret = target(*args, **kwargs)
  File "<stdin>", line 2, in t
Exception: a

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling d654c21 on mapix:master into ffa29fd on litl:master.

@mapix mapix force-pushed the master branch 2 times, most recently from 83847ac to eab391d Compare October 15, 2016 11:36
@coveralls
Copy link

coveralls commented Oct 15, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling 83847ac on mapix:master into ffa29fd on litl:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 83847ac on mapix:master into ffa29fd on litl:master.

@coveralls
Copy link

coveralls commented Oct 15, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling eab391d on mapix:master into ffa29fd on litl:master.

@bgreen-litl
Copy link
Member

Thank you for the PR.

As it turns out, we currently support a factor keyword argument on the backoff.expo wait generator. Your PR concerns making it available to the backoff.fibo wait generator as well. This seems like a good idea.

The way backoff handles parameterization of the wait generators is that any extra keyword arguments supplied in the decorator function declaration are passed to the wait generator at invocation time. For consistency with backoff.expo's support of factor, as well as in the interest of keeping the API surface as small as possible, I think this feature could be most cleanly implemented as an additional keyword arg to backoff.fibo.

Untested, but I think the implementation would be:

def fibo(max_value=None, factor=1):
    a = 1
    b = 1
    while True:
        if max_value is None or a * factor < max_value:
            yield a * factor
            a, b = b, a + b
        else:
            yield max_value

And to use it you'd pass factor as a keyword arg:

@backoff.on_exception(backoff.fibo, Exception, max_tries=4, factor=0.1, jitter=None)
# ...

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

Successfully merging this pull request may close these issues.

3 participants