User Contributed Modules¶
Rush’s Throttle Decorator¶
ThrottleDecorator
is an inferace which allows
Rush’s users to limit calls to a function using a decorator. Both
synchronous and asynchronous functions are supported.
-
class
rush.contrib.decorator.
ThrottleDecorator
(throttle: rush.throttle.Throttle)¶ The class that acts as a decorator used to throttle function calls.
This class requires an intantiated throttle with which to limit function invocations.
-
sleep_and_retry
(func: Callable) → Callable¶ Wrap function with a naive sleep and retry strategy.
Parameters: func (Callable) – The Callable
to decorate.Returns: Decorated function. Return type: Callable
-
Example¶
from rush import quota
from rush import throttle
from rush.contrib import decorator
from rush.limiters import periodic
from rush.stores import dictionary
t = throttle.Throttle(
limiter=periodic.PeriodicLimiter(
store=dictionary.DictionaryStore()
),
rate=quota.Quota.per_second(
count=1,
),
)
@decorator.ThrottleDecorator(throttle=t)
def ratelimited_func():
return True
try:
for _ in range(2):
ratelimited_func()
except decorator.ThrottleExceeded as e:
limit_result = e.result
print(limit_result.limited) # => True
print(limit_result.remaining) # => 0
print(limit_result.reset_after) # => ~0:00:01