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.)
    • you can restart the Redis server during the execution without any exception or losing any semaphore! (of course, if persistence is setup in the redis instance)
  • ✅ Very high performances with almost no polling at all
  • ✅ Memory backend (for testing)

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())
What about if you want to use the memory backend?

WARNING: the memory backend is just a wrapper on a classic asyncio.Semaphore, it is not "distributed" at all!

manager = DistributedSemaphoreManager(
    backend = "memory"
)

# and use it classically

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.3.tar.gz (7.0 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.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for async_redis_rate_limiters-0.0.3.tar.gz
Algorithm Hash digest
SHA256 da22b1c02b38771aa02275558d5b5ebf33df8c10602b35058cc7b0d5ae45c626
MD5 62ba5aef23e7c10e8860b9998e3ffd7d
BLAKE2b-256 b317a85df9593a2843f9658eb68fcd895425e37f9e13105d4fd0b7fa26444b24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for async_redis_rate_limiters-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bff93785f9d7c0ae0f87efccf005cc3da154aa257af4faf57d2f20749ca3309d
MD5 15408ab6c8e23e3bc1651a765917a578
BLAKE2b-256 1f8e92d0489e9492da35a6ffe1c31e2fec53619dffa0fd347aa9161ff4501e73

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