Skip to content

infrastructure.rate_limit.from_registry

src.infrastructure.rate_limit.from_registry

Generated rate limit rules from Route Metadata Registry.

This module generates RATE_LIMIT_RULES from the declarative ROUTE_REGISTRY, replacing hand-written rate limit configuration. Rules are automatically derived from RateLimitPolicy enums in the registry.

Two-Tier Configuration Pattern

Rate limits use a two-tier system (similar to CSS classes):

Tier 1 - Policy Assignment (registry.py): Each endpoint is assigned a policy category. Example: POST /api/v1/sessions → AUTH_LOGIN policy

Tier 2 - Policy Implementation (derivations.py): Each policy category has concrete limits. Example: AUTH_LOGIN → 5 attempts/min per IP

This file: Combines both tiers to generate the final rules dict.

To Modify Rate Limits

ONE endpoint: Update rate_limit_policy in registry.py ALL endpoints in a policy: Update RateLimitRule in derivations.py

Exports

RATE_LIMIT_RULES: Dict mapping endpoints to rate limit rules (auto-generated) get_rule_for_endpoint: Lookup function with path parameter matching

Usage

from src.infrastructure.rate_limit.from_registry import RATE_LIMIT_RULES

Lookup rule for endpoint

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

Reference
  • 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.

Classes

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