backoff icon indicating copy to clipboard operation
backoff copied to clipboard

add common factor for decay

Open mapix opened this issue 8 years ago • 5 comments

@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

mapix avatar Oct 15 '16 11:10 mapix

Coverage Status

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

coveralls avatar Oct 15 '16 11:10 coveralls

Coverage Status

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

coveralls avatar Oct 15 '16 11:10 coveralls

Coverage Status

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

coveralls avatar Oct 15 '16 11:10 coveralls

Coverage Status

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

coveralls avatar Oct 15 '16 11:10 coveralls

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)
# ...

bgreen-litl avatar Oct 18 '16 13:10 bgreen-litl