CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyrate-limiter

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

81

1.44x
Overview
Eval results
Files

task.mdevals/scenario-8/

Persistent Rate Limiter with Database Storage

Build a rate limiting system for an API service that uses persistent database storage to track request limits across server restarts and multiple processes. The system should enforce rate limits and maintain consistency even under concurrent access.

Requirements

Implement a rate limiter that:

  1. Uses PostgreSQL database for persistent storage of rate limit tracking data
  2. Enforces a rate limit of 10 requests per minute per API key
  3. Handles multiple concurrent requests safely without race conditions
  4. Fails quickly rather than waiting indefinitely when database locks cannot be acquired
  5. Provides a function to check if a request should be allowed
  6. Provides a function to reset rate limits for a specific API key

Implementation Details

Create a module rate_limiter.py that exports:

  • check_rate_limit(api_key: str) -> bool - Returns True if the request is allowed, False if rate limit exceeded
  • reset_rate_limit(api_key: str) -> None - Clears all rate limit data for the specified API key
  • close_limiter() -> None - Cleanup function to release database resources

The database connection should use these parameters:

  • Host: value from environment variable POSTGRES_HOST (default: "localhost")
  • Port: value from environment variable POSTGRES_PORT (default: 5432)
  • Database: value from environment variable POSTGRES_DB (default: "ratelimit")
  • User: value from environment variable POSTGRES_USER (default: "postgres")
  • Password: value from environment variable POSTGRES_PASSWORD (default: "postgres")

Test Cases

  • When checking rate limit for a new API key for the first time, it returns True @test
  • When checking rate limit 10 times within a minute for the same API key, all 10 checks return True @test
  • When checking rate limit 11 times within a minute for the same API key, the 11th check returns False @test
  • After resetting rate limits for an API key, checking rate limit returns True even if previously exceeded @test
  • After cleanup via close_limiter(), database connections are properly released @test

Dependencies { .dependencies }

pyrate-limiter { .dependency }

Provides rate limiting functionality with PostgreSQL storage backend.

@generates

API

def check_rate_limit(api_key: str) -> bool:
    """
    Check if a request from the given API key should be allowed.

    Args:
        api_key: The API key to check rate limits for

    Returns:
        True if the request is allowed, False if rate limit is exceeded
    """
    pass

def reset_rate_limit(api_key: str) -> None:
    """
    Reset rate limit data for the specified API key.

    Args:
        api_key: The API key to reset
    """
    pass

def close_limiter() -> None:
    """
    Release database resources and cleanup connections.
    """
    pass

Install with Tessl CLI

npx tessl i tessl/pypi-pyrate-limiter

tile.json