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-1/

API Rate Monitor

Build a diagnostic tool that monitors API rate limit violations and identifies which specific rate limits are being hit.

Problem Description

You need to create a rate monitoring system that tracks API requests and provides diagnostic information when rate limits are exceeded. The system should help developers understand which specific rate limit (per-second, per-minute, per-hour, etc.) is causing requests to be rejected.

Requirements

Rate Limit Configuration

The monitor should enforce multiple concurrent rate limits:

  • 5 requests per second
  • 20 requests per minute
  • 100 requests per hour

Monitoring Functionality

Implement a function monitor_api_request(user_id: str) -> dict that:

  • Attempts to acquire a permit for the given user
  • Returns a dictionary with:
    • allowed (bool): whether the request was allowed
    • user_id (str): the user identifier
    • failing_limit (str or None): description of which rate limit was exceeded (e.g., "5 per second", "20 per minute", or None if allowed)

Test Cases

  • When a user makes 6 requests in 1 second, the 6th request should be rejected with failing_limit indicating the per-second limit @test
  • When a user makes 21 requests spread over 30 seconds (staying under per-second limit), the 21st request should be rejected with failing_limit indicating the per-minute limit @test
  • When requests are within all rate limits, the monitor should return allowed=True and failing_limit=None @test

Implementation

@generates

API

def monitor_api_request(user_id: str) -> dict:
    """
    Monitor an API request and check against rate limits.

    Args:
        user_id: Identifier for the user making the request

    Returns:
        Dictionary with keys:
        - allowed (bool): Whether request was permitted
        - user_id (str): The user identifier
        - failing_limit (str|None): Description of exceeded limit or None
    """
    pass

Dependencies { .dependencies }

pyrate-limiter { .dependency }

Provides rate limiting with failure diagnostics.