mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-08-19 04:49:36 -07:00
Remove ratelimit library
This commit is contained in:
parent
b6c6590a12
commit
81e04269fd
2 changed files with 0 additions and 68 deletions
|
@ -1,59 +0,0 @@
|
||||||
from math import floor
|
|
||||||
|
|
||||||
import time
|
|
||||||
import sys
|
|
||||||
import threading
|
|
||||||
import functools
|
|
||||||
|
|
||||||
|
|
||||||
def clamp(value):
|
|
||||||
'''
|
|
||||||
Clamp integer between 1 and max
|
|
||||||
|
|
||||||
There must be at least 1 method invocation
|
|
||||||
made over the time period. Make sure the
|
|
||||||
value passed is at least 1 and is not a
|
|
||||||
fraction of an invocation.
|
|
||||||
|
|
||||||
:param float value: The number of method invocations.
|
|
||||||
:return: Clamped number of invocations.
|
|
||||||
:rtype: int
|
|
||||||
'''
|
|
||||||
return max(1, min(sys.maxsize, floor(value)))
|
|
||||||
|
|
||||||
|
|
||||||
class RateLimitDecorator:
|
|
||||||
def __init__(self, period=1, every=1.0):
|
|
||||||
self.frequency = abs(every) / float(clamp(period))
|
|
||||||
self.last_called = 0.0
|
|
||||||
self.lock = threading.RLock()
|
|
||||||
|
|
||||||
def __call__(self, func):
|
|
||||||
'''
|
|
||||||
Extend the behaviour of the following
|
|
||||||
function, forwarding method invocations
|
|
||||||
if the time window hes elapsed.
|
|
||||||
|
|
||||||
:param function func: The function to decorate.
|
|
||||||
:return: Decorated function.
|
|
||||||
:rtype: function
|
|
||||||
'''
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
'''Decorator wrapper function'''
|
|
||||||
with self.lock:
|
|
||||||
elapsed = time.time() - self.last_called
|
|
||||||
left_to_wait = self.frequency - elapsed
|
|
||||||
if left_to_wait > 0:
|
|
||||||
time.sleep(left_to_wait)
|
|
||||||
self.last_called = time.time()
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
rate_limited = RateLimitDecorator
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'rate_limited'
|
|
||||||
]
|
|
|
@ -1,9 +0,0 @@
|
||||||
class Version(object):
|
|
||||||
'''Version of the package'''
|
|
||||||
|
|
||||||
def __setattr__(self, *args):
|
|
||||||
raise TypeError('cannot modify immutable instance')
|
|
||||||
__delattr__ = __setattr__
|
|
||||||
|
|
||||||
def __init__(self, num):
|
|
||||||
super(Version, self).__setattr__('number', num)
|
|
Loading…
Add table
Add a link
Reference in a new issue