Refactor RateLimiter to allow proper subclassing

This commit is contained in:
Kristóf Tóth 2018-07-30 17:54:26 +02:00
parent f58f362e46
commit 6044f70804

View File

@ -1,25 +1,33 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from functools import wraps from functools import wraps, partial
from time import time, sleep from time import time, sleep
class RateLimiter: class RateLimiter:
def __init__(self, rate_per_second): def __init__(self, rate_per_second, action=sleep):
self.min_interval = 1 / float(rate_per_second) self.min_interval = 1 / float(rate_per_second)
self.action = action
self.fun = None
self.last_call = time() self.last_call = time()
def __call__(self, fun): def __call__(self, fun):
@wraps(fun) @wraps(fun)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
self._limit_rate() self.fun = partial(fun, *args, **kwargs)
fun(*args, **kwargs) limit_seconds = self._limit_rate()
if limit_seconds:
self.action(limit_seconds)
return
self.fun()
return wrapper return wrapper
def _limit_rate(self): def _limit_rate(self):
since_last_call = time() - self.last_call seconds_since_last_call = time() - self.last_call
to_next_call = self.min_interval - since_last_call seconds_to_next_call = self.min_interval - seconds_since_last_call
self.last_call = time() self.last_call = time()
if to_next_call > 0:
sleep(to_next_call) if seconds_to_next_call > 0:
return seconds_to_next_call
return 0