A simple and efficient rate limiting library implementing Token Bucket and Leaky Bucket algorithms.
Project description
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.
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 revlimiter-0.1.0.tar.gz.
File metadata
- Download URL: revlimiter-0.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acc4cc21c32c89f216ec320c631fcea55b549ed85992774486d0b065f09d0987
|
|
| MD5 |
54ee028ed95079f89fa7a3e421a8d9ab
|
|
| BLAKE2b-256 |
b303c84d57fd33b175cf907c3f2db03c69345654498ea9a285c97fb7a20351f8
|
File details
Details for the file revlimiter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: revlimiter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b06003063099d2b94b362f6b284baf19d9636f823c0371aa07b7b9c033686bf
|
|
| MD5 |
f7a94c884bcd08444115af0aa8bd2a4d
|
|
| BLAKE2b-256 |
12f6aac22af1ba9cb7695b157367528007544fe60823555503eb0bccde6dee12
|