Skip to content

infrastructure.rate_limit.config

src.infrastructure.rate_limit.config

Rate limit rules configuration (Generated from Registry).

IMPORTANT: This module is now a thin proxy to registry-generated rules. Rate limit rules are automatically generated from the Route Metadata Registry.

Two-Tier Configuration Pattern

Rate limits use a two-tier configuration similar to CSS classes:

Tier 1 - Policy Assignment (registry.py): Assigns a rate limit policy to each endpoint. Example: "Login endpoint should use AUTH_LOGIN policy"

To change: Update rate_limit_policy in RouteMetadata
Effect: Changes policy for ONE specific endpoint

Tier 2 - Policy Implementation (derivations.py): Defines the actual limits for each policy category. Example: "AUTH_LOGIN policy = 5 attempts per minute"

To change: Update RateLimitRule in _create_rate_limit_rule()
Effect: Changes limits for ALL endpoints using that policy

Examples:

Make login endpoint more generous (Tier 1 - one endpoint)

RouteMetadata( path="/sessions", rate_limit_policy=RateLimitPolicy.API_READ, # Change policy )

Increase AUTH_LOGIN limits globally (Tier 2 - all AUTH_LOGIN endpoints)

RateLimitPolicy.AUTH_LOGIN: RateLimitRule( max_tokens=10, # Change from 5 to 10 )

DO NOT add hand-written rules here - they will be ignored.

Usage

from src.infrastructure.rate_limit.config import RATE_LIMIT_RULES

Lookup rule for endpoint

rule = RATE_LIMIT_RULES.get("POST /api/v1/sessions")

Reference
  • src/infrastructure/rate_limit/from_registry.py (actual implementation)
  • src/presentation/routers/api/v1/routes/registry.py (Tier 1: policy assignment)
  • src/presentation/routers/api/v1/routes/derivations.py (Tier 2: policy implementation)

Attributes

RATE_LIMIT_RULES module-attribute

RATE_LIMIT_RULES: dict[str, RateLimitRule] = (
    build_rate_limit_rules(ROUTE_REGISTRY)
)

Endpoint to rate limit rule mapping (generated from registry).

Generated at module import time from ROUTE_REGISTRY. Endpoint format is "{METHOD} {PATH}" (e.g., "POST /api/v1/sessions").

To modify rate limits
  1. Update the RateLimitPolicy in ROUTE_REGISTRY
  2. Or update the policy mapping in derivations.py

DO NOT modify this dict directly - it will be regenerated on next import.

Functions

get_rule_for_endpoint

get_rule_for_endpoint(
    endpoint: str,
) -> RateLimitRule | None

Get rate limit rule for endpoint.

Supports exact match and path parameter patterns (e.g., /accounts/{id}).

Parameters:

Name Type Description Default
endpoint str

Endpoint string (e.g., "GET /api/v1/accounts/123").

required

Returns:

Type Description
RateLimitRule | None

RateLimitRule if found, None otherwise.

Example

rule = get_rule_for_endpoint("GET /api/v1/accounts/abc-123") rule.max_tokens 100

Source code in src/infrastructure/rate_limit/from_registry.py
def get_rule_for_endpoint(endpoint: str) -> RateLimitRule | None:
    """Get rate limit rule for endpoint.

    Supports exact match and path parameter patterns (e.g., /accounts/{id}).

    Args:
        endpoint: Endpoint string (e.g., "GET /api/v1/accounts/123").

    Returns:
        RateLimitRule if found, None otherwise.

    Example:
        >>> rule = get_rule_for_endpoint("GET /api/v1/accounts/abc-123")
        >>> rule.max_tokens
        100
    """
    # Try exact match first
    if endpoint in RATE_LIMIT_RULES:
        return RATE_LIMIT_RULES[endpoint]

    # Try pattern matching for path parameters
    method, _, path = endpoint.partition(" ")
    if not path:
        return None

    for pattern, rule in RATE_LIMIT_RULES.items():
        pattern_method, _, pattern_path = pattern.partition(" ")
        if method != pattern_method:
            continue

        # Check if paths match (handling {param} placeholders)
        if _paths_match(path, pattern_path):
            return rule

    return None