def __call__(self, units=1, waits=True, context=None): """To be called before consuming a resource that is subject to rate-limits. :param units: Number of units consumed by the API call that is about to be made. :param waits: If enable, then the member functions explictely calls `time.sleep` with the appropriate duration. :return: duration in seconds to wait for in order to stay under the rate limits policy :rtype: int """ self._load_state(context) state_changed = True now = timestamp_ms.now() if self.window is None: self.window = now elif self.window + self.__window_size < now: self.window = now self.count = 0 if self.count + units <= self.__calls_per_window: self.count += units eax = 0 else: eax = (self.window + self.__window_size - now) / 1e3 state_changed = False self._save_state(state_changed, context) if waits and eax != 0: LOGGER.debug('waiting %ss', eax) time.sleep(eax) return eax
def test_ensure_is_number(self): now = timestamp_ms.now() self.assertTrue(isinstance(now, int))
def test_from_now(self): self.assertGreater( timestamp_ms.now(), KIOTO_TIMESTAMP_MS )