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

Distributed API Rate Limiter

Build a distributed rate limiting system for a multi-instance API service that shares rate limit state across all server instances.

Requirements

Your system should implement a rate limiter that:

  1. Enforces a rate limit of 100 requests per minute per user across all server instances
  2. Uses a distributed storage backend to share rate limit state
  3. Provides both synchronous and asynchronous interfaces for rate limit checks
  4. Handles concurrent requests from multiple server instances correctly
  5. Returns whether a request is allowed, and if not, how long to wait

Implementation Details

Create a module with the following functionality:

  • A function to check if a user request should be allowed (synchronous)
  • A function to check if a user request should be allowed (asynchronous)
  • A function to reset rate limits for a specific user
  • A function to get the current request count for a user

The rate limiter should:

  • Accept a user identifier for each request
  • Track requests with millisecond precision
  • Automatically clean up expired rate limit data
  • Work correctly when multiple server instances access the same storage

Test Cases

  • Allows 100 requests from a user within one minute @test
  • Blocks the 101st request from a user within the same minute @test
  • Allows requests again after the minute window expires @test
  • Correctly handles concurrent requests from different server instances using async interface @test

Implementation

@generates

API

def check_rate_limit(user_id: str) -> tuple[bool, int]:
    """
    Check if a request from the user should be allowed.

    Parameters:
    - user_id: Unique identifier for the user

    Returns:
    - Tuple of (allowed: bool, wait_time_ms: int)
      - allowed: True if request is allowed, False otherwise
      - wait_time_ms: Time to wait in milliseconds before retrying (0 if allowed)
    """

async def check_rate_limit_async(user_id: str) -> tuple[bool, int]:
    """
    Asynchronously check if a request from the user should be allowed.

    Parameters:
    - user_id: Unique identifier for the user

    Returns:
    - Tuple of (allowed: bool, wait_time_ms: int)
      - allowed: True if request is allowed, False otherwise
      - wait_time_ms: Time to wait in milliseconds before retrying (0 if allowed)
    """

def reset_user_limit(user_id: str) -> None:
    """
    Reset rate limit state for a specific user.

    Parameters:
    - user_id: Unique identifier for the user
    """

def get_user_count(user_id: str) -> int:
    """
    Get the current request count for a user within the current time window.

    Parameters:
    - user_id: Unique identifier for the user

    Returns:
    - Current count of requests for the user
    """

Dependencies { .dependencies }

pyrate-limiter { .dependency }

Provides distributed rate limiting capabilities with Redis backend support.

redis { .dependency }

Python Redis client for connecting to Redis server.