Skip to main content

Rock solid async python generic distributed rate limiters (concurrency and time) backed by Redis.

Project description

async-redis-rate-limiters

Python Badge UV Badge Mergify Badge Renovate Badge MIT Licensed

Rock solid async python generic distributed rate limiters (concurrency and time) backed by Redis.

[!WARNING]
This is a very preliminary version of the library and only concurrency limiters are available for now.

Features

  • ✅ Support very high concurrency (>100K), keep a reasonable number of connections to Redis (default: 300)
  • ✅ Rock solid with Redis/Network failures (multiple attempts, exponential backoff, etc.)
  • ✅ Very high performances with almost no polling at all

Non-features

  • ❌ No time based rate limiters (yet)
  • ❌ No blocking support, only async Python

Installation

pip install async-redis-rate-limiters

(or same with your favorite package manager)

Usage

import asyncio
from typing import AsyncContextManager
from async_redis_rate_limiters import DistributedSemaphoreManager


async def worker(semaphore: AsyncContextManager):
    async with semaphore:
        # concurrency limit enforced here
        pass


async def main():
    manager = DistributedSemaphoreManager(
        redis_url="redis://localhost:6379",
        redis_max_connections=100,
    )
    # Limit the concurrency to 10 concurrent tasks for the key "test"
    semaphore = manager.get_semaphore("test", 10)
    tasks = [asyncio.create_task(worker(semaphore)) for _ in range(1000)]
    await asyncio.gather(*tasks)


if __name__ == "__main__":
    asyncio.run(main())

Dev

  • Lint the code:

make lint

  • Run the tests:

make test

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

async_redis_rate_limiters-0.0.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_redis_rate_limiters-0.0.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file async_redis_rate_limiters-0.0.1.tar.gz.

File metadata

File hashes

Hashes for async_redis_rate_limiters-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a3bcc3363344b5d1d83fb6e02906b784c032980aacaac80c66f9cb9ba9c5cfa6
MD5 7f230d17b0a3b6e4e86f0a2311b13c02
BLAKE2b-256 716cf13888145aec41a891675044a8c4a5d920eadcf8e373cae54103ef39d694

See more details on using hashes here.

File details

Details for the file async_redis_rate_limiters-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for async_redis_rate_limiters-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a233a485cb598ce5b8bc6172e03ab242a8a2662aeee8971af7ebada9e1e28924
MD5 9dc81f5bb4955aef5bc7c4db185f771c
BLAKE2b-256 b25b51c52e1ad55e3a5d8dc9b0736df4747fd4cfe5714d675c83ee1d55d1f0cb

See more details on using hashes here.

Supported by

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