Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioredis_rate_limiter-0.1.0.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

aioredis_rate_limiter-0.1.0-py3-none-any.whl (3.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page