def test_full_jitter(self, values): jittered = list(full_jitter(values)) self.assertThat(jittered, AllMatch(IsInstance(float))) self.assertThat( jittered, AllMatch(MatchesAll(GreaterThanOrEqual(0.0), LessThan(10000.0))), )
def gen_retry_intervals(base=0.01, rate=2.5, maximum=10.0): """Generate retry intervals based on an exponential series. Once any interval exceeds `maximum` the interval generated will forever be `maximum`; this effectively disconnects from the exponential series. All intervals will be subject to "jitter" as a final step. The defaults seem like reasonable coefficients for a capped, full-jitter, exponential back-off series, and were derived by experimentation at the command-line. Real-world experience may teach us better values. """ # An exponentially growing series... intervals = exponential_growth(base, rate) # from which we stop pulling one we've hit a maximum... intervals = takewhile((lambda i: i < maximum), intervals) # and thereafter return the maximum value indefinitely... intervals = chain(intervals, repeat(maximum)) # and to which we add some randomness. return full_jitter(intervals)