Python Rate-Limiter using Leaky-Bucket Algorithm for controlling request rates in applications with multiple backend storage options.
81
Build a simple request timing monitor that tracks elapsed time between requests using a monotonic clock source to ensure accuracy even when system time changes.
Your task is to implement a RequestMonitor class that:
The monitor should work correctly even if the system clock is adjusted (e.g., due to NTP synchronization or daylight saving time changes).
Create a RequestMonitor class with the following behavior:
record_request(): Records the timestamp of a new requestget_last_elapsed(): Returns the elapsed time in milliseconds since the previous request, or None if this is the first requestget_average_interval(): Returns the average time interval in milliseconds between all recorded requests, or None if fewer than 2 requests have been recordedget_request_count(): Returns the total number of recorded requests@generates
class RequestMonitor:
"""Monitors request timing using monotonic clock source."""
def __init__(self):
"""Initialize a new request monitor."""
pass
def record_request(self) -> None:
"""Record a new request timestamp."""
pass
def get_last_elapsed(self) -> float | None:
"""
Get elapsed time in milliseconds since the previous request.
Returns:
Elapsed time in milliseconds, or None if this is the first request.
"""
pass
def get_average_interval(self) -> float | None:
"""
Get average time interval in milliseconds between all requests.
Returns:
Average interval in milliseconds, or None if fewer than 2 requests.
"""
pass
def get_request_count(self) -> int:
"""
Get the total number of recorded requests.
Returns:
Number of recorded requests.
"""
passProvides monotonic clock implementation for accurate timing.
@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