def test_calculate_backoff(self): sec = utils.calculate_backoff(0, 10, 2, 0) self.assertEquals(sec, 0) sec = utils.calculate_backoff(9, 10, 2, 0) self.assertEquals(sec, 1.8) sec = utils.calculate_backoff(4, 10, 2, 0) self.assertEquals(sec, 0.8) sec = utils.calculate_backoff(4, 10, 2, 1) if sec != 0.8: self.assertThat(sec, matchers.GreaterThan(0.8)) self.assertThat(sec, matchers.LessThan(1.8)) self.assertRaises(ValueError, utils.calculate_backoff, 0, 10, -2, -1) self.assertRaises(ValueError, utils.calculate_backoff, 0, 10, -2, 0) self.assertRaises(ValueError, utils.calculate_backoff, 0, 10, 2, -1) self.assertRaises(ValueError, utils.calculate_backoff, -2, -10, 2, 0) self.assertRaises(ValueError, utils.calculate_backoff, 2, -10, 2, 0) self.assertRaises(ValueError, utils.calculate_backoff, -2, 10, 2, 0) self.assertRaises(ValueError, utils.calculate_backoff, -1, 10, 2, 0) self.assertRaises(ValueError, utils.calculate_backoff, 10, 10, 2, 0) self.assertRaises(ValueError, utils.calculate_backoff, 11, 10, 2, 0)
def _backoff_sleep(self, attempt): """Sleep between retries using a jitter algorithm. Mitigates thrashing between multiple parallel requests, and creates backpressure on clients to slow down the rate at which they submit requests. :param attempt: current attempt number, zero-based """ seconds = utils.calculate_backoff(attempt, options.CFG.max_attempts, options.CFG.max_retry_sleep, options.CFG.max_retry_jitter) time.sleep(seconds)