import asyncio import re import time from collections.abc import Awaitable, Callable from dataclasses import dataclass from uuid import uuid4 from starlette.exceptions import HTTPException from starlette.types import ASGIApp, Message, Receive, Scope, Send from messenger.shared.application.errors import Unavailable from messenger.shared.application.ports import Logger from messenger.shared.entrypoints.errors import INSTANCE_ID_HEADER, MESSAGES, REQUEST_ID_HEADER from messenger.shared.entrypoints.schemas import ErrorEnvelope type Headers = tuple[tuple[str, str], ...] @dataclass(frozen=True, slots=True) class Rejection: status: int code: str headers: Headers = () type Guard = Callable[[Scope], Awaitable[Rejection | None]] type BodyLimits = tuple[tuple[re.Pattern[str], int], ...] HEADER_BYTES = REQUEST_ID_HEADER.lower().encode() INSTANCE_BYTES = INSTANCE_ID_HEADER.lower().encode() STAMPED = frozenset({HEADER_BYTES, INSTANCE_BYTES}) MAX_REQUEST_ID = 200 def header(scope: Scope, name: bytes) -> str: for key, value in scope["headers"]: if key == name: return value.decode("latin-1").strip() return "" async def send_error( send: Send, status: int, code: str, request_id: str, headers: Headers = () ) -> None: body = ErrorEnvelope.of(code, MESSAGES[code], request_id).model_dump_json().encode() await send( { "type": "http.response.start", "status": status, "headers": [ (b"content-type", b"application/json"), (b"content-length", str(len(body)).encode()), (HEADER_BYTES, request_id.encode("latin-1", "replace")), *((name.encode(), value.encode("latin-1")) for name, value in headers), ], } ) await send({"type": "http.response.body", "body": body}) class Pace: __slots__ = ("_deadline", "_moved", "_opened", "_rate", "_timeout") def __init__(self, deadline: asyncio.Timeout, timeout: float, rate: float) -> None: self._deadline = deadline self._timeout = timeout self._rate = rate self._opened = asyncio.get_running_loop().time() self._moved = 0 def around(self, receive: Receive, send: Send) -> tuple[Receive, Send]: async def paced_receive() -> Message: return self._count(await receive()) async def paced_send(message: Message) -> None: await send(self._count(message)) return paced_receive, paced_send def _count(self, message: Message) -> Message: if size := len(message.get("body", b"")): self._moved += size moved = min(asyncio.get_running_loop().time(), self._opened + self._moved / self._rate) self._deadline.reschedule(moved + self._timeout) return message class RequestPipeline: __slots__ = ( "_app", "_guard", "_instance", "_limits", "_logger", "_max_bytes", "_min_rate", "_timeout", ) def __init__( self, app: ASGIApp, logger: Logger, max_bytes: int, timeout: float, guard: Guard, instance: str, limits: BodyLimits = (), min_rate: float = 131_072, ) -> None: self._app = app self._logger = logger self._max_bytes = max_bytes self._limits = limits self._timeout = timeout self._min_rate = min_rate self._guard = guard self._instance = instance.encode() async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if scope["type"] != "http": await self._app(scope, receive, send) return incoming = header(scope, HEADER_BYTES) request_id = incoming[:MAX_REQUEST_ID] if incoming else uuid4().hex scope.setdefault("state", {}).update(request_id=request_id, actor=None) started = time.perf_counter() status_holder = [500] answered = [False] async def wrapped_send(message: Message) -> None: if message["type"] == "http.response.start": status_holder[0] = message["status"] answered[0] = True headers = [ (key, value) for key, value in message.get("headers", []) if key.lower() not in STAMPED ] headers.append((HEADER_BYTES, request_id.encode("latin-1", "replace"))) headers.append((INSTANCE_BYTES, self._instance)) message = {**message, "headers": headers} await send(message) limit = self._limit(scope["path"]) received = [0] async def limited_receive() -> Message: message = await receive() received[0] += len(message.get("body", b"")) if received[0] > limit: raise HTTPException(413) return message rejection: Rejection | None = None try: async with asyncio.timeout(self._timeout) as deadline: rejection = await self._guard(scope) or self._oversized(scope, limit) if rejection is None: pace = Pace(deadline, self._timeout, self._min_rate) await self._app(scope, *pace.around(limited_receive, wrapped_send)) except Unavailable as error: self._logger.warning( "dependency.unavailable", route=self._route(scope), error=type(error.__cause__ or error).__name__, request_id=request_id, ) rejection = Rejection(503, "unavailable") except (TimeoutError, asyncio.CancelledError) as error: cancelled = isinstance(error, asyncio.CancelledError) self._logger.warning( "request.cancelled" if cancelled else "request.timeout", method=scope["method"], route=self._route(scope), request_id=request_id, limit=None if cancelled else self._timeout, duration_ms=round((time.perf_counter() - started) * 1000, 3), ) if not answered[0]: await send_error(wrapped_send, 503, "unavailable", request_id) self._log(scope, status_holder[0], request_id, started) if cancelled: raise return except Exception: self._logger.error( "request.unhandled", method=scope["method"], route=self._route(scope), request_id=request_id, duration_ms=round((time.perf_counter() - started) * 1000, 3), ) raise if rejection is not None and not answered[0]: await send_error( wrapped_send, rejection.status, rejection.code, request_id, rejection.headers ) self._log(scope, status_holder[0], request_id, started) def _limit(self, path: str) -> int: for pattern, limit in self._limits: if pattern.fullmatch(path): return limit return self._max_bytes def _oversized(self, scope: Scope, limit: int) -> Rejection | None: declared = header(scope, b"content-length") if declared.isdigit() and int(declared) > limit: return Rejection(413, "payload_too_large") return None def _route(self, scope: Scope) -> str: route = scope.get("route") return getattr(route, "path", scope["path"]) def _log(self, scope: Scope, status: int, request_id: str, started: float) -> None: self._logger.info( "request.handled", method=scope["method"], route=self._route(scope), status=status, request_id=request_id, duration_ms=round((time.perf_counter() - started) * 1000, 3), )