Token Bucket Rate Limiter
The `token-bucket` package provides a very fast implementation of the token bucket algorithm, suitable for rate limiting in web applications. It manages bucket state internally without requiring a separate timer thread, allowing for controlled bursts while maintaining a consistent average rate. The current version is 0.3.0, and releases appear to be infrequent, focusing on stability and core algorithm improvements.
Warnings
- breaking Version 0.3.0 dropped support for Python 2.x and now requires Python 3.5 or newer. Users on older Python versions must upgrade their environment or stick to `token-bucket<0.3.0`.
- gotcha The `token-bucket` library primarily provides an in-memory, single-process token bucket implementation. For distributed rate limiting across multiple application instances (e.g., in a microservices architecture), you will need to implement a shared state mechanism (e.g., using Redis) or use a different library designed for distributed rate limiting.
- gotcha Prior to version 0.3.0, rates less than 1.0 token per second might not have been handled correctly. Version 0.3.0 explicitly added support for fractional rates, allowing for finer-grained control over low-frequency limits.
Install
-
pip install token-bucket
Imports
- TokenBucket
from token_bucket import TokenBucket
Quickstart
from token_bucket import TokenBucket
import time
# Create a token bucket with a capacity of 10 tokens,
# refilling at a rate of 2 tokens per second.
# This allows for a burst of 10 requests, then 2 requests/second.
bucket = TokenBucket(capacity=10, rate=2.0)
print(f"Initial tokens: {bucket.tokens}")
# Try to consume 5 tokens
if bucket.consume(5):
print("Consumed 5 tokens successfully.")
else:
print("Failed to consume 5 tokens.")
print(f"Tokens after first consumption: {bucket.tokens}")
# Wait a bit for tokens to refill
time.sleep(2.0)
# Try to consume 8 tokens (after 2 seconds, 4 tokens should have refilled)
if bucket.consume(8):
print("Consumed 8 tokens successfully after refill.")
else:
print("Failed to consume 8 tokens after refill.")
print(f"Tokens after second consumption: {bucket.tokens}")