2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-07-30 15:54:26 +00:00
|
|
|
from functools import wraps, partial
|
2018-02-13 16:56:01 +00:00
|
|
|
from time import time, sleep
|
|
|
|
|
2018-07-30 16:04:24 +00:00
|
|
|
from tfw.decorators.lazy_property import lazy_property
|
|
|
|
|
2018-02-13 16:56:01 +00:00
|
|
|
|
|
|
|
class RateLimiter:
|
2018-07-30 15:54:26 +00:00
|
|
|
def __init__(self, rate_per_second, action=sleep):
|
2018-02-13 16:56:01 +00:00
|
|
|
self.min_interval = 1 / float(rate_per_second)
|
2018-07-30 15:54:26 +00:00
|
|
|
self.action = action
|
|
|
|
self.fun = None
|
2018-02-13 16:56:01 +00:00
|
|
|
self.last_call = time()
|
|
|
|
|
|
|
|
def __call__(self, fun):
|
|
|
|
@wraps(fun)
|
|
|
|
def wrapper(*args, **kwargs):
|
2018-07-30 15:54:26 +00:00
|
|
|
self.fun = partial(fun, *args, **kwargs)
|
|
|
|
limit_seconds = self._limit_rate()
|
|
|
|
if limit_seconds:
|
|
|
|
self.action(limit_seconds)
|
|
|
|
return
|
|
|
|
self.fun()
|
2018-02-13 16:56:01 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
def _limit_rate(self):
|
2018-07-30 15:54:26 +00:00
|
|
|
seconds_since_last_call = time() - self.last_call
|
|
|
|
seconds_to_next_call = self.min_interval - seconds_since_last_call
|
2018-02-13 16:56:01 +00:00
|
|
|
self.last_call = time()
|
2018-07-30 15:54:26 +00:00
|
|
|
|
|
|
|
if seconds_to_next_call > 0:
|
|
|
|
return seconds_to_next_call
|
|
|
|
return 0
|
2018-07-30 15:55:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AsyncRateLimiter(RateLimiter):
|
2018-07-30 16:04:24 +00:00
|
|
|
def __init__(self, rate_per_second, ioloop_factory):
|
|
|
|
self._ioloop_factory = ioloop_factory
|
|
|
|
self._ioloop = None
|
|
|
|
self._last_callback = None
|
|
|
|
|
2018-07-30 15:55:52 +00:00
|
|
|
super().__init__(
|
|
|
|
rate_per_second=rate_per_second,
|
|
|
|
action=self.async_action
|
|
|
|
)
|
|
|
|
|
2018-07-30 16:04:24 +00:00
|
|
|
@lazy_property
|
|
|
|
def ioloop(self):
|
|
|
|
return self._ioloop_factory()
|
|
|
|
|
2018-07-30 15:55:52 +00:00
|
|
|
def async_action(self, seconds_to_next_call):
|
2018-07-30 16:04:24 +00:00
|
|
|
if self._last_callback:
|
|
|
|
self.ioloop.remove_timeout(self._last_callback)
|
2018-07-30 15:55:52 +00:00
|
|
|
|
2018-07-30 16:04:24 +00:00
|
|
|
self._last_callback = self.ioloop.call_later(
|
2018-07-30 15:55:52 +00:00
|
|
|
seconds_to_next_call,
|
|
|
|
self.fun
|
|
|
|
)
|