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 async_redis_rate_limiters import DistributedSemaphoreManager


async def worker(manager: DistributedSemaphoreManager):
    # Limit the concurrency to 10 concurrent tasks for the key "test"
    async with manager.get_semaphore("test", 10):
        # concurrency limit enforced here
        pass


async def main():
    manager = DistributedSemaphoreManager(
        redis_url="redis://localhost:6379",
        redis_max_connections=100,
        redis_ttl=3600,  # semaphore max duration (seconds)
    )
    tasks = [asyncio.create_task(worker(manager)) 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

note: you need a redis instance listening to localhost:6379

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.1.1.tar.gz (8.2 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.1.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for async_redis_rate_limiters-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7953dc44f93a55496201bcadc4a98f6e615a2f343607ed27e102c85b1b348225
MD5 6fb06222fb41ae34c5f0ffb90cf70941
BLAKE2b-256 69b19d49cb8aa63222c5579d0775e3278c0aa21518f8a676ff9b4a274922fece

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for async_redis_rate_limiters-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a3dc7d9697cf4a5abb1f53422300c5f972fe5fa9abcf50e1c2263ffab83699db
MD5 899f3e4c06b1104588c9fbcd3b85ab60
BLAKE2b-256 d2b7b17bc275b4999c7c4b99670d1cd14e5da71f6d84f31d2d9be0acd3e7f57c

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