Rush: Throttling Algorithms
Rush is a Python library that provides a composable and extensible framework for implementing various rate limiting algorithms and storage backends. It includes a periodic interval rate limiter, a leaky bucket rate limiter (Generic Cell Ratelimiting Algorithm - GCRA), and supports Redis and in-memory dictionary storage. The library emphasizes type annotations and requires Python 3.6 or newer. The current version is 2021.4.0, released in April 2021.
Warnings
- gotcha The library's API is explicitly described as 'preliminary' and 'experimental'. While current imports are promised not to break, 'porcelain' (higher-level abstractions) may be added in future versions, potentially altering the recommended usage patterns.
- gotcha The last release (2021.4.0) was in April 2021. While the project is marked as 'Production/Stable', development activity appears to be infrequent. Users seeking actively developed or frequently updated libraries might need to consider this pace.
Install
-
pip install rush
Imports
- quota
from rush import quota
- throttle
from rush import throttle
- PeriodicLimiter
from rush.limiters import periodic
- DictionaryStore
from rush.stores import dictionary
Quickstart
from rush import quota
from rush import throttle
from rush.limiters import periodic
from rush.stores import dictionary
t = throttle.Throttle(
limiter=periodic.PeriodicLimiter(
store=dictionary.DictionaryStore()
),
rate=quota.Quota.per_hour(
count=5000,
burst=500,
),
)
limit_result = t.check('expensive-operation/user@example.com', 1)
print(f"Limited: {limit_result.limited}")
print(f"Remaining: {limit_result.remaining}")
print(f"Reset after: {limit_result.reset_after}")