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
¶
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
- Update the RateLimitPolicy in ROUTE_REGISTRY
- 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 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