Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ashita-ai_tessera /src /tessera /api /rate_limit.py
| """Rate limiting configuration and dependencies. | |
| Provides two levels of rate limiting: | |
| 1. Per-API-key rate limiting (default): Each API key has its own bucket | |
| 2. Per-team rate limiting: Aggregated limits across all keys for a team | |
| Per-team rate limiting is used for expensive operations like schema diff | |
| and lineage analysis to prevent a team from monopolizing resources. | |
| """ | |
| import hashlib | |
| from collections.abc import Awaitable, Callable | |
| from functools import wraps | |
| from typing import ParamSpec, TypeVar | |
| from fastapi import Request, Response | |
| from fastapi.responses import JSONResponse | |
| from slowapi import Limiter | |
| from slowapi.util import get_remote_address | |
| from tessera.config import settings | |
| P = ParamSpec("P") | |
| T = TypeVar("T") | |
| # Hash key length for rate limit key generation | |
| # Using first 16 characters of SHA256 hex digest provides sufficient uniqueness | |
| # while keeping keys compact (collision probability < 2^-64) | |
| HASH_KEY_LENGTH = 16 | |
| def get_rate_limit_key(request: Request) -> str: | |
| """Get a unique key for rate limiting. | |
| Uses a hash of the full API key if present in the Authorization header, | |
| otherwise falls back to remote IP address. Prefixes with actor type | |
| (agent/human) so agent-tier rate limits can be applied. | |
| Note: We hash the full key to ensure each API key gets its own rate limit | |
| bucket, preventing one noisy client from affecting others. | |
| """ | |
| is_agent = getattr(getattr(request, "state", None), "is_agent", False) | |
| actor_prefix = "agent" if is_agent else "human" | |
| auth_header = request.headers.get("Authorization") | |
| if auth_header and auth_header.startswith("Bearer "): | |
| api_key = auth_header[7:] | |
| # Hash the full API key to create a unique, stable bucket per key | |
| # Using SHA256 and taking first HASH_KEY_LENGTH chars for a compact but unique key | |
| key_hash = hashlib.sha256(api_key.encode()).hexdigest()[:HASH_KEY_LENGTH] | |
| return f"{actor_prefix}:key:{key_hash}" | |
| # Fallback to IP address | |
| return f"{actor_prefix}:{get_remote_address(request)}" | |
| def get_team_rate_limit_key(request: Request) -> str: | |
| """Get a rate limit key based on the authenticated team. | |
| This groups all API keys belonging to the same team into one bucket, | |
| preventing a team from bypassing rate limits by creating multiple keys. | |
| Falls back to API key hash if team_id is not available in request state. | |
| """ | |
| # Check if team_id was set during request processing | |
| team_id = getattr(request.state, "team_id", None) | |
| if team_id: | |
| return f"team:{team_id}" | |
| # Fallback to API key-based limiting | |
| return get_rate_limit_key(request) | |
| # Initialize limiter for per-API-key rate limiting | |
| # Rate limiting can be disabled via settings.rate_limit_enabled | |
| # Note: enabled must be a boolean, not a callable. We'll control it via middleware. | |
| limiter = Limiter( | |
| key_func=get_rate_limit_key, | |
| enabled=True, # Always enabled at limiter level, controlled via middleware | |
| default_limits=[lambda: settings.rate_limit_global], | |
| ) | |
| # Initialize a separate limiter for per-team rate limiting | |
| # Used for expensive operations that should be limited per team, not per key | |
| team_limiter = Limiter( | |
| key_func=get_team_rate_limit_key, | |
| enabled=True, | |
| default_limits=[], # No default, only used explicitly | |
| ) | |
| def _get_retry_after(exc: Exception) -> int: | |
| """Extract Retry-After seconds from the rate limit that was exceeded. | |
| Parses the rate limit window from slowapi's RateLimitExceeded exception. | |
| Falls back to 60 seconds if the window cannot be determined. | |
| """ | |
| try: | |
| # slowapi sets exc.limit (a Limit wrapper) with exc.limit.limit (a RateLimitItem) | |
| limit_wrapper = getattr(exc, "limit", None) | |
| if limit_wrapper is not None: | |
| rate_limit_item = getattr(limit_wrapper, "limit", None) | |
| if rate_limit_item is not None: | |
| expiry = rate_limit_item.get_expiry() | |
| if expiry > 0: | |
| return int(expiry) | |
| except Exception: | |
| pass | |
| return 60 | |
| def rate_limit_exceeded_handler(request: Request, exc: Exception) -> Response: | |
| """Custom handler for rate limit exceeded errors. | |
| Adds the 'Retry-After' header derived from the rate limit window. | |
| """ | |
| detail = str(getattr(exc, "detail", str(exc))) | |
| retry_after = _get_retry_after(exc) | |
| response = JSONResponse( | |
| status_code=429, | |
| content={ | |
| "error": { | |
| "code": "RATE_LIMIT_EXCEEDED", | |
| "message": "Too Many Requests", | |
| "detail": detail, | |
| } | |
| }, | |
| ) | |
| response.headers["Retry-After"] = str(retry_after) | |
| return response | |
| # Helper decorators for common scopes using callables for dynamic settings | |
| # When rate limiting is disabled, use empty string to disable limits | |
| def _get_rate_limit(limit_str: str) -> str: | |
| """Get rate limit string, or empty string if rate limiting is disabled.""" | |
| return limit_str if settings.rate_limit_enabled else "" | |
| def _agent_aware_limit(human_attr: str, agent_attr: str) -> Callable[[str], str]: | |
| """Create a limit function that selects agent or human tier based on the key. | |
| slowapi calls key_func(request) first to produce a key string, then passes | |
| that key to this function if it has a ``key`` parameter. Since | |
| get_rate_limit_key prefixes the key with "agent:" or "human:", we can | |
| inspect the key to pick the right tier without needing the request object. | |
| """ | |
| def _limit_func(key: str) -> str: | |
| if not settings.rate_limit_enabled: | |
| return "" | |
| if key.startswith("agent:"): | |
| return str(getattr(settings, agent_attr)) | |
| return str(getattr(settings, human_attr)) | |
| return _limit_func | |
| # Per-API-key rate limit decorators with agent tier support | |
| limit_read = limiter.limit( | |
| _agent_aware_limit("rate_limit_read", "rate_limit_agent_read"), | |
| key_func=get_rate_limit_key, | |
| ) | |
| limit_write = limiter.limit( | |
| _agent_aware_limit("rate_limit_write", "rate_limit_agent_write"), | |
| key_func=get_rate_limit_key, | |
| ) | |
| limit_admin = limiter.limit( | |
| _agent_aware_limit("rate_limit_admin", "rate_limit_agent_admin"), | |
| key_func=get_rate_limit_key, | |
| ) | |
| limit_auth = limiter.limit(lambda: _get_rate_limit(settings.rate_limit_auth)) | |
| # Per-team rate limit decorators for expensive operations | |
| limit_expensive = team_limiter.limit(lambda: _get_rate_limit(settings.rate_limit_expensive)) | |
| limit_bulk = team_limiter.limit(lambda: _get_rate_limit(settings.rate_limit_bulk)) | |
| def set_team_id_in_request( | |
| func: Callable[P, Awaitable[T]], | |
| ) -> Callable[P, Awaitable[T]]: | |
| """Decorator that sets team_id in request state after auth resolves. | |
| This must be applied AFTER the auth dependency resolves. It extracts | |
| the team_id from the auth context and stores it in request.state for | |
| use by per-team rate limiting. | |
| Usage: | |
| @router.get("/expensive") | |
| @limit_expensive # Per-team rate limit | |
| @set_team_id_in_request | |
| async def expensive_operation(request: Request, auth: Auth, ...): | |
| ... | |
| """ | |
| async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: | |
| # Find auth and request in kwargs | |
| request = kwargs.get("request") | |
| auth = kwargs.get("auth") | |
| if request is not None and auth is not None: | |
| # Set team_id in request state for rate limiting | |
| # request is a FastAPI Request object with state attribute | |
| if hasattr(request, "state") and hasattr(auth, "team_id"): | |
| request.state.team_id = str(auth.team_id) # type: ignore[union-attr] | |
| return await func(*args, **kwargs) | |
| return wrapper | |
| def combined_rate_limit( | |
| per_key_limit: str | None = None, | |
| per_team_limit: str | None = None, | |
| ) -> Callable[[Callable[P, Awaitable[T]]], Callable[P, Awaitable[T]]]: | |
| """Apply both per-key and per-team rate limits to an endpoint. | |
| This is useful for expensive operations that need both types of limiting: | |
| - Per-key prevents a single API key from making too many requests | |
| - Per-team prevents a team from bypassing limits with multiple keys | |
| Args: | |
| per_key_limit: Limit per API key (e.g., "100/minute") | |
| per_team_limit: Limit per team (e.g., "20/minute") | |
| Usage: | |
| @router.get("/diff") | |
| @combined_rate_limit(per_key_limit="100/minute", per_team_limit="20/minute") | |
| async def diff_schemas(request: Request, auth: Auth, ...): | |
| ... | |
| """ | |
| def decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]: | |
| wrapped = func | |
| # Apply per-team limit if specified | |
| if per_team_limit: | |
| team_limit_decorator = team_limiter.limit(lambda: _get_rate_limit(per_team_limit)) | |
| wrapped = team_limit_decorator(set_team_id_in_request(wrapped)) | |
| # Apply per-key limit if specified | |
| if per_key_limit: | |
| key_limit_decorator = limiter.limit(lambda: _get_rate_limit(per_key_limit)) | |
| wrapped = key_limit_decorator(wrapped) | |
| return wrapped | |
| return decorator | |
Xet Storage Details
- Size:
- 9.15 kB
- Xet hash:
- 1a7cc411def06376485ea8cd075432fe4a4cf19f6c240cde4682240a94cfc51e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.