Rate limiter implements on Redis
Project description
aioredis-rate-limiter
Rate limiter implements on Redis
Implementation of distributed rate limiter with aioredis, an asyncio based redis client.
Simple rate limiter based on Redis DB.
The key features are:
- Concurrency work
- Rate by limit requests
- Rate by time for requests
Usage
We want to limit requests from 5 pods to limited resource. Limits for resource: no more 10 request per 20 seconds.
locker = AioRedisRateLimiter(redis, rate_limit=10, rate_key_ttl=20)
import asyncio
import os
from aioredis.client import Redis
from aioredis_rate_limiter import AioRedisRateLimiter
class Executor:
def __init__(self, name: str, locker: AioRedisRateLimiter, task_count: int = 10):
self._locker = locker
self._task_count = task_count
self._name = name
async def process(self):
for i in range(self._task_count):
while True:
is_ok = await self._locker.acquire()
if is_ok:
print(f'Executor {self._name} by {i+1}')
break
else:
await asyncio.sleep(1)
async def main():
host = os.getenv('REDIS_HOST')
db = os.getenv('REDIS_DB')
redis = Redis.from_url(host, db=db, encoding="utf-8", decode_responses=True)
locker = AioRedisRateLimiter(redis, rate_limit=10, rate_key_ttl=15)
w1 = Executor('first', locker, 10)
w2 = Executor('helper', locker, 8)
w3 = Executor('lazzy', locker, 5)
tasks = [w1.process(), w2.process(), w3.process()]
await asyncio.gather(*tasks)
if __name__ == '__main__':
asyncio.run(main())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for aioredis_rate_limiter-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7158f50a4fa53a48b2d739df3331b6b13fb0c500fe6106219f36859ed5c9f2ea |
|
MD5 | a9cdf589cf3799393b4f5452c1bf3bb7 |
|
BLAKE2b-256 | 9a29b513638b102f388d4b3a8d2bc45792fb376481170addb7b37ad85fa8fc41 |
Close
Hashes for aioredis_rate_limiter-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 983d03e87bd8a6e2b2fa61008658e4b60881440400bfa915fa29a905e5bb4f6f |
|
MD5 | c586947f568845f6b9bc6eb701adb626 |
|
BLAKE2b-256 | 4b1d23e9035ce1f59c3671c6bd4e63bd2d3b70fab0ebb7f5d1ba4be927843235 |