Skip to main content

Rate limiter for async functions using Redis as a backend

Project description

asyncio-redis-rate-limit

wemake.services test codecov Python Version wemake-python-styleguide

Rate limiter for async functions using Redis as a backend.

Features

  • Small and simple
  • Can be used as a decorator or as a context manager
  • Can be used for both clients and servers
  • Works with asyncio
  • Works with any amount of processes
  • Works with both redis.asyncio.client.Redis and aioredis
  • Free of race-conditions (hopefully!)
  • Supports redis since 7.0
  • Fully typed with annotations and checked with mypy, PEP561 compatible

Installation

pip install asyncio-redis-rate-limit

Extras available:

  • pip install asyncio-redis-rate-limit[redis]
  • pip install asyncio-redis-rate-limit[aioredis] (for python versions <3.11)

Example

As a decorator:

>>> from asyncio_redis_rate_limit import rate_limit, RateSpec
>>> from redis.asyncio import Redis as AsyncRedis  # pip install redis

>>> redis = AsyncRedis.from_url('redis://localhost:6379')

>>> @rate_limit(
...    rate_spec=RateSpec(requests=1200, seconds=60),
...    backend=redis,
... )
... async def request() -> ...:
...     ...   # Do something useful! Call this function as usual.

Or as a context manager:

>>> from asyncio_redis_rate_limit import RateLimiter, RateSpec
>>> from redis.asyncio import Redis as AsyncRedis  # pip install redis

>>> redis = AsyncRedis.from_url('redis://localhost:6379')

>>> async def request() -> ...:
...     async with RateLimiter(
...         unique_key='api-name.com',
...         backend=redis,
...         rate_spec=RateSpec(requests=5, seconds=1),
...     ):
...         ...  # Do the request itself.

License

MIT

Credits

This project was generated with wemake-python-package. Current template version is: 1d63652fbb33ebe2f6d932f511b7f529a4ce2d2a. See what is updated since then.

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

asyncio_redis_rate_limit-1.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

asyncio_redis_rate_limit-1.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_redis_rate_limit-1.1.0.tar.gz.

File metadata

  • Download URL: asyncio_redis_rate_limit-1.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.11.9 Darwin/24.6.0

File hashes

Hashes for asyncio_redis_rate_limit-1.1.0.tar.gz
Algorithm Hash digest
SHA256 12b176456b07b24b2244fee258bd6e35cf9a11d67c199a1826cda9a1299b4229
MD5 38fd2836f02a2987eecfeb2991b88505
BLAKE2b-256 76c3994e0fef58df8545a66943139bdeb6636fedfb392a8c890e48b4e8cf45a2

See more details on using hashes here.

File details

Details for the file asyncio_redis_rate_limit-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_redis_rate_limit-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6c5553b603b50073868def1bba898715b5aafbb4dbd1f80b7172115c62aedf1
MD5 c511d71073f84c5565b502ef00ce0966
BLAKE2b-256 5b68c73908b3d482bc286d9611592c533d9bda680076a7a0370b0e8679f29f44

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