https://github.com/aymenjd/py-redis-ratelimit
A simple asyncio-based rate limiter for python using Redis.
https://github.com/aymenjd/py-redis-ratelimit
asyncio rate-limit redis spam
Last synced: 8 months ago
JSON representation
A simple asyncio-based rate limiter for python using Redis.
- Host: GitHub
- URL: https://github.com/aymenjd/py-redis-ratelimit
- Owner: AYMENJD
- License: mit
- Created: 2022-06-17T05:31:45.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-06T16:05:10.000Z (over 3 years ago)
- Last Synced: 2025-01-29T01:40:06.243Z (over 1 year ago)
- Topics: asyncio, rate-limit, redis, spam
- Language: Python
- Homepage:
- Size: 7.81 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# py-redis-ratelimit [](https://pypi.org/project/py-redis-ratelimit) [](https://pypistats.org/packages/py-redis-ratelimit)
A simple asynchronous rate limiter based on redis.
### Requirements
- python >= 3.7
- [redis-py](https://github.com/redis/redis-py) >= 4.2.0
### Installation
```bash
pip install py-redis-ratelimit
```
### Examples
Basic example:
```python
from redis.asyncio import Redis
import ratelimit, asyncio
redis = Redis(decode_responses=True)
limiter = ratelimit.RateLimit(
redis, prefix="api_rate_limit", rate=10, period=60, retry_after=20
)
print(ratelimit.RateLimit.__doc__) # print RateLimit class docstring
async def do_something():
await limiter.acquire(
identifier="do_something_function"
) # a unique identifier for the function. This let's RateLimit know what service/resource you are trying to access.
...
async def main():
for x in range(40):
try:
print("Calling do_something() for the {}th time".format(x + 1))
await do_something()
except ratelimit.FloodWait as e:
print("Exception:", e.to_dict())
break
if __name__ == "__main__":
asyncio.run(main())
```
# Contributing
Pull requests are always welcome!!
# License
MIT [License](https://github.com/AYMENJD/py-redis-ratelimit/blob/main/LICENSE)