Frequency limit for asyncio
Project description
aiofreqlimit — Async GCRA rate limiter
Async rate limiting for Python 3.11+ built on the Generic Cell Rate Algorithm (GCRA) with type-safe parameters and pluggable backends.
Installation
pip install aiofreqlimit
Quickstart
Create a contract (FreqLimitParams), choose a backend, and wrap your code with the
async context manager:
import asyncio
from aiofreqlimit import FreqLimit, FreqLimitParams
from aiofreqlimit.backends.memory import InMemoryBackend
params = FreqLimitParams(limit=1, period=1.0, burst=1) # 1 op / second
limiter = FreqLimit(params, backend=InMemoryBackend())
async def send_message(chat_id: int, text: str) -> None:
async with limiter.resource(f"chat:{chat_id}"):
await bot.send_message(chat_id, text)
async def main() -> None:
await asyncio.gather(*(send_message(42, f"msg {i}") for i in range(5)))
asyncio.run(main())
keyis any hashable;Noneuses a global bucket.burstlets you allow short bursts without changing the long-term rate.
Params are type-safe
You can keep your limits as constants and reuse them across the project:
from aiofreqlimit import FreqLimitParams
TELEGRAM_PER_CHAT = FreqLimitParams(limit=1, period=1.0, burst=1)
TELEGRAM_PER_GROUP = FreqLimitParams(limit=20, period=60.0, burst=3)
Backends
InMemoryBackend(in-process, single event loop) — import fromaiofreqlimit.backends.memory.idle_ttl: float | None— drop idle keys after this many seconds (default: None).sweeper_interval: float | None— optional background cleanup period; set to enable a sweeper task (default: None).
RedisBackend(shared, multi-host) — import fromaiofreqlimit.backends.redis.- Install optional deps:
pip install aiofreqlimit[redis]. - Uses Redis server time and a Lua script for atomic GCRA steps.
prefix: str— key prefix (defaultfreqlimit:gcra:).extra_ttl: float— small buffer added to debt horizon; controls how long keys stay after backlog is cleared.
- Install optional deps:
- Implement
FreqLimitBackendto plug in other stores:
from collections.abc import Hashable
from aiofreqlimit import FreqLimitBackend, FreqLimitParams
class RedisBackend(FreqLimitBackend):
async def reserve(self, key: Hashable, now: float, params: FreqLimitParams) -> float:
...
FreqLimit requires an explicit backend instance; no default is provided.
Testing
The library ships with pytest + hypothesis tests. To run them with uv:
uv run pytest tests
Integration tests for the Redis backend use Testcontainers; Docker must be available for those cases.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiofreqlimit-0.2.2.tar.gz.
File metadata
- Download URL: aiofreqlimit-0.2.2.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d028d692d14ca6c5ec979841076876df74b5731e924cdf90f321f9673b71ff45
|
|
| MD5 |
a74aef3d68d11ae265adc4467ef2ba11
|
|
| BLAKE2b-256 |
4683794bb481104251a4d3170f672a98bc3359d4b75b2021bb817734343de4ca
|
File details
Details for the file aiofreqlimit-0.2.2-py3-none-any.whl.
File metadata
- Download URL: aiofreqlimit-0.2.2-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9db66a4e401bb560331a9fc20410a2e9be2a23001cb726ea66295b1a461e87d5
|
|
| MD5 |
a215befc12c48fd39ec254604c1ccd3f
|
|
| BLAKE2b-256 |
1641af8102e3d1e825ebb58de6ef9c23b5c589c23acfe613f1763495f7ded52a
|