Suor / funcy

A fancy and practical functional tools
BSD 3-Clause "New" or "Revised" License
3.35k stars 142 forks source link

feat($retry): enable capped exponential backoff and jitter via keyword arguments #119

Closed RainrainWu closed 10 months ago

RainrainWu commented 2 years ago

Summary

Checklist

Reason

Suor commented 1 year ago

Thanks for the PR and sorry for a very slow response.

The thing is adding extra params to retry feels like a way to nowhere. Also, timeout already accepts callables so one might simply:

@retry(..., timeout=lambda attempt: min(60, 2 ** attempt * random.uniform(0.5, 2)))
def some_func(...):
    pass

# Or if need it many times:

def exp_backoff(exp, jitter_amp=1, cap=float("inf")): 
    return lambda attempt: min(exp ** attempt * random.uniform(1.0 / jitter_amp, jitter_amp), cap) 

@retry(..., timeout=exp_backoff(2, jitter_amp=2, cap=60))
def some_func(...):
    pass

Which will reach the same purpose easier.

Suor commented 1 year ago

The above exp_backoff() might be included into funcy on its own of cause. It is a oneliner though.

RainrainWu commented 1 year ago

@Suor Thanks for the reply!

I totally agree that the current implementation is much more flexible, the PR is trying to look for a more straightforward approach to getting started :)

It is ok to be closed if the design does not align with your expectation 😃