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

Multi-Tenant API Rate Limiter

Build a rate limiting system for a multi-tenant API service that enforces per-tenant rate limits with different tiers.

Requirements

The system should support three tenant tiers with different rate limits:

  • Basic tier: 10 requests per minute
  • Premium tier: 50 requests per minute
  • Enterprise tier: 200 requests per minute

Implement a rate limiter that:

  • Routes requests to separate buckets based on tenant ID
  • Dynamically creates buckets for new tenants as needed
  • Uses in-memory storage for simplicity
  • Blocks when rate limits are exceeded

Test Cases

  • A basic tenant can make 10 requests within a minute successfully @test
  • A basic tenant making 11 requests within a minute is blocked on the 11th request @test
  • A premium tenant can make 50 requests within a minute successfully @test
  • Multiple tenants with the same tier have independent rate limits @test

Implementation

@generates

API

class TenantTier:
    """Enum for tenant tiers"""
    BASIC = "basic"
    PREMIUM = "premium"
    ENTERPRISE = "enterprise"

class TenantRateLimiterFactory:
    """Factory that routes rate limiting by tenant ID and tier"""

    def __init__(self):
        """Initialize the factory"""
        pass

    def register_tenant(self, tenant_id: str, tier: str) -> None:
        """
        Register a tenant with a specific tier.

        Args:
            tenant_id: Unique identifier for the tenant
            tier: Tier level (basic, premium, or enterprise)
        """
        pass

    def acquire(self, tenant_id: str) -> bool:
        """
        Try to acquire a rate limit permit for a tenant.

        Args:
            tenant_id: The tenant requesting access

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

Dependencies { .dependencies }

pyrate-limiter { .dependency }

Provides rate limiting functionality with bucket-based algorithms.

Install with Tessl CLI

npx tessl i tessl/pypi-pyrate-limiter

tile.json