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
Metadata
Release files for aiofreqlimit 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| aiofreqlimit-0.2.2.tar.gz | 8.2 kB | Without provenance | Details for aiofreqlimit-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| aiofreqlimit-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for aiofreqlimit-0.2.2-py3-none-any.whl |
Total release size: 18.9 kB
Release files / aiofreqlimit-0.2.2.tar.gz
| Download URL | aiofreqlimit-0.2.2.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d028d692d14ca6c5ec979841076876df74b5731e924cdf90f321f9673b71ff45
|
|
BLAKE2b-256 checksum How to use checksums |
4683794bb481104251a4d3170f672a98bc3359d4b75b2021bb817734343de4ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / aiofreqlimit-0.2.2-py3-none-any.whl
| Download URL | aiofreqlimit-0.2.2-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9db66a4e401bb560331a9fc20410a2e9be2a23001cb726ea66295b1a461e87d5
|
|
BLAKE2b-256 checksum How to use checksums |
1641af8102e3d1e825ebb58de6ef9c23b5c589c23acfe613f1763495f7ded52a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Security for aiofreqlimit 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.