Python Rate-Limiter using Leaky-Bucket Algorithm for controlling request rates in applications with multiple backend storage options.
81
A simple API request tracking system that enforces rate limits on incoming requests.
Build a request tracker that manages API request limits for different services. The tracker should enforce rate limits and provide feedback about whether requests can be processed or should be delayed.
The tracker should:
@generates
class RequestTracker:
"""
Tracks API requests and enforces rate limits.
Args:
max_requests: Maximum number of requests allowed
time_window_seconds: Time window for the rate limit in seconds
"""
def __init__(self, max_requests: int, time_window_seconds: int):
pass
def process_request(
self,
request_name: str,
blocking: bool = True,
timeout: float | None = None
) -> bool:
"""
Process a request and enforce rate limits.
Args:
request_name: Identifier for the request
blocking: If True, wait for rate limit to allow request.
If False, return immediately if rate limit exceeded.
timeout: Maximum time to wait in blocking mode (seconds).
None means wait indefinitely.
Returns:
True if request can proceed, False if rejected due to rate limit
"""
pass
def close(self):
"""Clean up resources."""
passProvides rate limiting functionality.
Install with Tessl CLI
npx tessl i tessl/pypi-pyrate-limiterdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10