2020-02-18 07:39:43 +00:00
|
|
|
import threading
|
2018-12-18 06:10:00 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
class Ratelimiter:
|
2020-02-18 07:40:42 +00:00
|
|
|
'''
|
|
|
|
The Ratelimiter class is used to limit how often you perform some other
|
|
|
|
action. Just create a Ratelimiter object with the allowance you need, then
|
|
|
|
call `limit()` before doing the thing you wish to ratelimit.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
download_limiter = Ratelimiter(allowance=1, period=3)
|
|
|
|
|
|
|
|
for file_url in file_urls:
|
|
|
|
download_limiter.limit()
|
|
|
|
download(file_url)
|
|
|
|
'''
|
2018-12-18 06:10:00 +00:00
|
|
|
def __init__(self, allowance, period=1, operation_cost=1, mode='sleep'):
|
|
|
|
'''
|
|
|
|
allowance:
|
|
|
|
Our spending balance per `period` seconds.
|
|
|
|
|
|
|
|
period:
|
|
|
|
The number of seconds over which we can perform `allowance` operations.
|
|
|
|
|
|
|
|
operation_cost:
|
|
|
|
The default amount to remove from our balance after each operation.
|
|
|
|
Pass a `cost` parameter to `self.limit` to use a nondefault value.
|
|
|
|
|
|
|
|
mode:
|
|
|
|
'sleep':
|
2020-02-18 07:40:42 +00:00
|
|
|
If we do not have the balance for an operation, sleep until we
|
|
|
|
do. Then return True every time.
|
2018-12-18 06:10:00 +00:00
|
|
|
|
|
|
|
'reject':
|
2020-02-18 07:40:42 +00:00
|
|
|
If we do not have the balance for an operation, do nothing and
|
|
|
|
return False. Otherwise subtract the cost and return True.
|
|
|
|
|
|
|
|
Although (allowance=1, period=1) and (allowance=30, period=30) can both
|
|
|
|
be described as "once per second", the latter allows for much greater
|
|
|
|
burstiness of operation. You could spend the whole allowance in a
|
|
|
|
single second, then relax for 29 seconds, for example.
|
2018-12-18 06:10:00 +00:00
|
|
|
'''
|
|
|
|
if mode not in ('sleep', 'reject'):
|
|
|
|
raise ValueError('Invalid mode %s' % repr(mode))
|
|
|
|
|
|
|
|
self.allowance = allowance
|
|
|
|
self.period = period
|
|
|
|
self.operation_cost = operation_cost
|
|
|
|
self.mode = mode
|
2020-02-18 07:39:43 +00:00
|
|
|
self.lock = threading.Lock()
|
2018-12-18 06:10:00 +00:00
|
|
|
|
|
|
|
self.last_operation = time.time()
|
|
|
|
self.balance = 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def gain_rate(self):
|
|
|
|
return self.allowance / self.period
|
|
|
|
|
2020-02-18 07:39:43 +00:00
|
|
|
def _limit(self, cost):
|
2018-12-18 06:10:00 +00:00
|
|
|
time_diff = time.time() - self.last_operation
|
|
|
|
self.balance += time_diff * self.gain_rate
|
|
|
|
self.balance = min(self.balance, self.allowance)
|
|
|
|
|
|
|
|
if self.balance >= cost:
|
|
|
|
self.balance -= cost
|
2020-02-18 07:01:15 +00:00
|
|
|
successful = True
|
2020-02-18 07:04:39 +00:00
|
|
|
|
|
|
|
elif self.mode == 'reject':
|
|
|
|
successful = False
|
|
|
|
|
2018-12-18 06:10:00 +00:00
|
|
|
else:
|
2020-02-18 07:04:39 +00:00
|
|
|
deficit = cost - self.balance
|
|
|
|
time_needed = deficit / self.gain_rate
|
|
|
|
time.sleep(time_needed)
|
|
|
|
self.balance = 0
|
|
|
|
successful = True
|
2018-12-18 06:10:00 +00:00
|
|
|
|
|
|
|
self.last_operation = time.time()
|
2020-02-18 07:01:15 +00:00
|
|
|
return successful
|
2020-02-18 07:39:43 +00:00
|
|
|
|
|
|
|
def limit(self, cost=None):
|
|
|
|
'''
|
|
|
|
See the main class docstring for info about cost and mode behavior.
|
|
|
|
'''
|
|
|
|
if cost is None:
|
|
|
|
cost = self.operation_cost
|
|
|
|
|
|
|
|
with self.lock:
|
|
|
|
return self._limit(cost)
|