or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pyrate-limiter@3.9.x
tile.json

tessl/pypi-pyrate-limiter

tessl install tessl/pypi-pyrate-limiter@3.9.0

Python Rate-Limiter using Leaky-Bucket Algorithm for controlling request rates in applications with multiple backend storage options.

Agent Success

Agent success rate when using this tile

81%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.45x

Baseline

Agent success rate without this tile

56%

task.mdevals/scenario-7/

Request Timing Monitor

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.

Requirements

Your task is to implement a RequestMonitor class that:

  1. Records request timestamps using a monotonic clock source (immune to system clock adjustments)
  2. Calculates elapsed time between consecutive requests in milliseconds
  3. Returns timing statistics for the recorded requests

The monitor should work correctly even if the system clock is adjusted (e.g., due to NTP synchronization or daylight saving time changes).

Implementation Details

Create a RequestMonitor class with the following behavior:

  • record_request(): Records the timestamp of a new request
  • get_last_elapsed(): Returns the elapsed time in milliseconds since the previous request, or None if this is the first request
  • get_average_interval(): Returns the average time interval in milliseconds between all recorded requests, or None if fewer than 2 requests have been recorded
  • get_request_count(): Returns the total number of recorded requests

Test Cases

  • A new monitor has zero requests recorded @test
  • Recording a single request updates the count to 1 @test
  • Recording two requests allows calculating elapsed time between them @test
  • Recording multiple requests (3+) allows calculating average interval @test
  • Elapsed time uses monotonic clock and is unaffected by system time adjustments @test

Implementation

@generates

API

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.
        """
        pass

Dependencies { .dependencies }

pyrate-limiter { .dependency }

Provides monotonic clock implementation for accurate timing.

@satisfied-by