In-memory rate limiter with sliding window and token bucket
Project description
philiprehberger-rate-limiter
In-memory rate limiter with sliding window and token bucket algorithms.
Installation
pip install philiprehberger-rate-limiter
Usage
Basic Rate Limiting
from philiprehberger_rate_limiter import RateLimiter, Algorithm
limiter = RateLimiter(
requests=100,
window_seconds=60,
algorithm=Algorithm.SLIDING_WINDOW,
)
if limiter.allow("user-123"):
handle_request()
Check Status
status = limiter.status("user-123")
print(f"Allowed: {status.allowed}")
print(f"Remaining: {status.remaining}/{status.limit}")
print(f"Resets at: {status.reset_at}")
Blocking Wait
# Synchronous — blocks until quota is available or timeout expires
status = limiter.wait("user-123", timeout=5.0)
# Async — awaits until quota is available
status = await limiter.async_acquire("user-123")
Decorator
limiter = RateLimiter(10, 60)
@limiter.limit("10/minute")
def api_endpoint():
return {"data": "ok"}
Algorithms
# Fixed window — resets at interval boundaries
RateLimiter(100, 60, Algorithm.FIXED_WINDOW)
# Sliding window (default) — rolling time window
RateLimiter(100, 60, Algorithm.SLIDING_WINDOW)
# Token bucket — smooth rate with burst capacity
RateLimiter(100, 60, Algorithm.TOKEN_BUCKET)
API
| Name | Description |
|---|---|
RateLimiter(requests, window_seconds, algorithm=SLIDING_WINDOW) |
Create a rate limiter |
limiter.allow(key) |
Check if request is allowed |
limiter.status(key) |
Get detailed LimitStatus |
limiter.wait(key, timeout=10.0) |
Block until quota available or raise RateLimitExceeded |
limiter.async_acquire(key) |
Async wait until quota is available |
limiter.reset(key) |
Reset state for a key |
limiter.reset_all() |
Reset state for all keys |
limiter.active_keys() |
List all keys with active state |
limiter.limit(rate) |
Decorator with rate string (e.g., "10/minute") |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this package useful, consider starring the repository.
License
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 philiprehberger_rate_limiter-0.3.0.tar.gz.
File metadata
- Download URL: philiprehberger_rate_limiter-0.3.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b195666b785ae04615ce6c38deb50fdd4f2fff29199981434283c1c04ba9f5e1
|
|
| MD5 |
934907c2f6c94f5262b24877b6406d56
|
|
| BLAKE2b-256 |
6a58ffffe755497dc8b8ca12c5dd2d404cdbfd28a6e41bb83eb4255ce2897424
|
File details
Details for the file philiprehberger_rate_limiter-0.3.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_rate_limiter-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3f0f93dfa5461ef8d5a6791ca8682033877e7f8ba577b5788b1fe740d7c7d38
|
|
| MD5 |
ababbc385c48de155829f2b32f92f266
|
|
| BLAKE2b-256 |
78a9ffaf84bcb794316c6e1a69b4799bcfac799767b3b34c27733b1204ece2e2
|