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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file aioredis_rate_limiter-0.1.0.tar.gz.

File metadata

  • Download URL: aioredis_rate_limiter-0.1.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for aioredis_rate_limiter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7158f50a4fa53a48b2d739df3331b6b13fb0c500fe6106219f36859ed5c9f2ea
MD5 a9cdf589cf3799393b4f5452c1bf3bb7
BLAKE2b-256 9a29b513638b102f388d4b3a8d2bc45792fb376481170addb7b37ad85fa8fc41

See more details on using hashes here.

File details

Details for the file aioredis_rate_limiter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aioredis_rate_limiter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 983d03e87bd8a6e2b2fa61008658e4b60881440400bfa915fa29a905e5bb4f6f
MD5 c586947f568845f6b9bc6eb701adb626
BLAKE2b-256 4b1d23e9035ce1f59c3671c6bd4e63bd2d3b70fab0ebb7f5d1ba4be927843235

See more details on using hashes here.

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