revlimiter
A simple and efficient Python library for rate limiting, implementing popular algorithms like Token Bucket and Leaky Bucket.
Features
- Token Bucket: Allows for bursts of requests while maintaining a steady average rate.
- Leaky Bucket: Smooths out bursts by processing requests at a constant rate.
- RateLimitManager: Easy management of multiple clients and algorithms.
- Type-safe: Built with Python type hints.
- Lightweight: Zero external dependencies.
Installation
pip install revlimiter
Quick Start
Using RateLimitManager
The RateLimitManager is the easiest way to manage rate limits for multiple users or clients.
from revlimiter import RateLimitManager
# Create a manager with Token Bucket algorithm
manager = RateLimitManager(
algorithm="token-bucket",
capacity=10,
refill_rate=2.0 # 2 tokens per second
)
# Get bucket for a specific client
client_id = "user_123"
bucket = manager.get_bucket(client_id)
if bucket.allow_request():
print("Request allowed!")
else:
print("Rate limit exceeded.")
Direct Algorithm Usage
Token Bucket
from revlimiter import TokenBucket
# 10 tokens capacity, 1 token refilled every second
bucket = TokenBucket(capacity=10, refill_rate=1.0)
if bucket.allow_request(tokens=1):
# Process request
pass
Leaky Bucket
from revlimiter import LeakyBucket
# 5 requests capacity, leaks 1 request every 2 seconds (0.5 per sec)
bucket = LeakyBucket(capacity=5, leak_rate=0.5)
if bucket.allow_request():
# Process request
pass
API Reference
TokenBucket(capacity: int, refill_rate: float)
allow_request(tokens: int = 1) -> bool: Consumes tokens and returnsTrueif successful.get_remaining() -> int: Returns current available tokens.
LeakyBucket(capacity: int, leak_rate: float)
allow_request(requests: int = 1) -> bool: Adds requests to bucket and returnsTrueif successful.get_remaining() -> int: Returns current remaining capacity.
RateLimitManager(algorithm: str, **kwargs_config)
get_bucket(client_id: str): Returns a bucket for the given client.
License
MIT License. See LICENSE for details.
Metadata
Release files for revlimiter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| revlimiter-0.1.0.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| revlimiter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / revlimiter-0.1.0.tar.gz
| Download URL | revlimiter-0.1.0.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
acc4cc21c32c89f216ec320c631fcea55b549ed85992774486d0b065f09d0987
|
|
BLAKE2b-256 checksum How to use checksums |
b303c84d57fd33b175cf907c3f2db03c69345654498ea9a285c97fb7a20351f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / revlimiter-0.1.0-py3-none-any.whl
| Download URL | revlimiter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b06003063099d2b94b362f6b284baf19d9636f823c0371aa07b7b9c033686bf
|
|
BLAKE2b-256 checksum How to use checksums |
12f6aac22af1ba9cb7695b157367528007544fe60823555503eb0bccde6dee12
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|