Python Rate-Limiter using Leaky-Bucket Algorithm for controlling request rates in applications with multiple backend storage options.
81
Build a simple API request tracking system that monitors and controls the rate of API calls to ensure compliance with rate limits.
Create a request tracker that limits the number of API requests within specified time windows. The system should support both blocking mode (wait until a request can proceed) and non-blocking mode (immediately return whether a request is allowed). The tracker should enforce rate limits and provide clear feedback about request acceptance.
Implement a function can_make_request(client_id, blocking=True) that checks if a request from a specific client can proceed. Each client has its own rate limit configured as follows:
Test cases:
True when first request from "client_1" is made (within 5/minute limit) @testFalse in non-blocking mode when "client_2" makes 4th request within same second (exceeds 3/second limit) @testTrue when "client_3" makes 3rd request in blocking mode, waiting until second elapses @testdef can_make_request(client_id: str, blocking: bool = True) -> bool:
"""
Check if a request from the specified client can proceed.
Args:
client_id: Unique identifier for the client making the request
blocking: If True, wait until request can proceed; if False, return immediately
Returns:
True if request is allowed, False otherwise (only in non-blocking mode)
"""
pass@generates
Provides rate limiting capabilities for controlling request rates with configurable time windows.
@satisfied-by
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