baseimage-tutorial-framework/lib/tfw/decorators/rate_limiter.py

101 lines
3.3 KiB
Python
Raw Normal View History

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from functools import wraps, partial
2018-02-13 16:56:01 +00:00
from time import time, sleep
from tfw.decorators.lazy_property import lazy_property
2018-02-13 16:56:01 +00:00
class RateLimiter:
2018-07-31 03:19:15 +00:00
"""
2018-07-31 07:19:42 +00:00
Decorator class for rate limiting, blocking.
2018-07-31 03:19:15 +00:00
When applied to a function this decorator will apply rate limiting
if the function is invoked more frequently than rate_per_seconds.
By default rate limiting means sleeping until the next invocation time
as per __init__ parameter rate_per_seconds.
Note that this decorator BLOCKS THE THREAD it is being executed on,
so it is only acceptable for stuff running on a separate thread.
If this is no good for you please refer to AsyncRateLimiter in this module,
2018-07-31 09:48:41 +00:00
which is designed not to block and use the IOLoop it is being called from.
2018-07-31 03:19:15 +00:00
"""
2018-07-31 09:48:41 +00:00
def __init__(self, rate_per_second):
2018-07-31 03:19:15 +00:00
"""
:param rate_per_second: max frequency the decorated method should be
invoked with
"""
2018-02-13 16:56:01 +00:00
self.min_interval = 1 / float(rate_per_second)
self.fun = None
2018-02-13 16:56:01 +00:00
self.last_call = time()
2018-07-31 09:48:41 +00:00
def action(self, seconds_to_next_call):
if seconds_to_next_call:
sleep(seconds_to_next_call)
self.fun()
2018-02-13 16:56:01 +00:00
def __call__(self, fun):
@wraps(fun)
def wrapper(*args, **kwargs):
self.fun = partial(fun, *args, **kwargs)
limit_seconds = self._limit_rate()
2018-07-31 09:48:41 +00:00
self.action(limit_seconds)
2018-02-13 16:56:01 +00:00
return wrapper
def _limit_rate(self):
seconds_since_last_call = time() - self.last_call
seconds_to_next_call = self.min_interval - seconds_since_last_call
if seconds_to_next_call > 0:
return seconds_to_next_call
2018-07-31 07:55:48 +00:00
self.last_call = time()
return 0
class AsyncRateLimiter(RateLimiter):
2018-07-31 07:19:42 +00:00
"""
Decorator class for rate limiting, non-blocking.
2018-07-31 09:48:41 +00:00
The semantics of the rate limiting:
- unlike RateLimiter this decorator never blocks, instead it adds an async
callback version of the decorated function to the IOLoop
(to be executed after the rate limiting has expired).
- the timing works similarly to RateLimiter
2018-07-31 07:19:42 +00:00
"""
def __init__(self, rate_per_second, ioloop_factory):
2018-07-31 07:19:42 +00:00
"""
:param rate_per_second: max frequency the decorated method should be
invoked with
:param ioloop_factory: callable that should return an instance of the
IOLoop of the application
"""
self._ioloop_factory = ioloop_factory
self._ioloop = None
self._last_callback = None
2018-07-31 07:14:33 +00:00
self._make_action_thread_safe()
2018-07-31 09:48:41 +00:00
super().__init__(rate_per_second=rate_per_second)
2018-07-31 07:14:33 +00:00
def _make_action_thread_safe(self):
2018-07-31 09:48:41 +00:00
self.action = partial(self.ioloop.add_callback, self.action)
2018-07-31 07:14:33 +00:00
@lazy_property
def ioloop(self):
return self._ioloop_factory()
2018-07-31 09:48:41 +00:00
def action(self, seconds_to_next_call):
if self._last_callback:
self.ioloop.remove_timeout(self._last_callback)
self._last_callback = self.ioloop.call_later(
seconds_to_next_call,
2018-07-31 09:48:41 +00:00
self.fun_with_debounce
)
2018-07-31 09:48:41 +00:00
def fun_with_debounce(self):
self.last_call = time()
self.fun()