From 95d408986e07926ae25665d44887e8853be1f659 Mon Sep 17 00:00:00 2001 From: Queenfish Date: Thu, 15 Oct 2026 19:46:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8:=20=D0=BA?= =?UTF-8?q?=D1=8D=D1=88,=20cursor,=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=BA=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/chats.py | 21 +++++++- app/api/users.py | 4 +- app/cache.py | 4 +- app/main.py | 90 +++++++++++++++++++++++++++++++++- app/security.py | 20 ++++++-- app/session_store.py | 4 +- app/storage.py | 33 ++++++++----- evidence/cache_experiment.json | 26 ++++++++++ 8 files changed, 179 insertions(+), 23 deletions(-) diff --git a/app/api/chats.py b/app/api/chats.py index f867db9..926b66a 100644 --- a/app/api/chats.py +++ b/app/api/chats.py @@ -1,3 +1,5 @@ +import uuid + from fastapi import APIRouter, Header, HTTPException, Query, Request from fastapi.responses import JSONResponse @@ -27,11 +29,25 @@ def error_response( ) +def validate_uuid(value: str) -> None: + try: + uuid.UUID(value) + except (ValueError, TypeError): + raise HTTPException(status_code=400, detail="invalid uuid") + + async def resolve_actor(request: Request, authorization: str | None): rid = request.state.request_id try: user, _ = await get_current_user(request, authorization) - except HTTPException: + except HTTPException as exc: + if exc.status_code == 503: + return None, error_response( + 503, + "unavailable", + "auth store unavailable", + rid, + ) return None, error_response( 401, "unauthorized", @@ -96,6 +112,7 @@ async def get_chat( authorization: str | None = Header(default=None), ): rid = request.state.request_id + validate_uuid(chat_id) actor, err = await resolve_actor(request, authorization) if err is not None: return err @@ -113,6 +130,7 @@ async def create_message( authorization: str | None = Header(default=None), ): rid = request.state.request_id + validate_uuid(chat_id) actor, err = await resolve_actor(request, authorization) if err is not None: return err @@ -134,6 +152,7 @@ async def list_messages( cursor: str | None = Query(None), ): rid = request.state.request_id + validate_uuid(chat_id) actor, err = await resolve_actor(request, authorization) if err is not None: return err diff --git a/app/api/users.py b/app/api/users.py index cc31c1d..eabcf2e 100644 --- a/app/api/users.py +++ b/app/api/users.py @@ -45,7 +45,9 @@ async def current_user( rid = request.state.request_id try: user, _ = await get_current_user(request, authorization) - except HTTPException: + except HTTPException as exc: + if exc.status_code == 503: + return error_response(503, "unavailable", "auth store unavailable", rid) return error_response( 401, "unauthorized", diff --git a/app/cache.py b/app/cache.py index 004ea51..86058f3 100644 --- a/app/cache.py +++ b/app/cache.py @@ -54,7 +54,7 @@ def make_cache() -> Cache: client = redis.from_url( config.CACHE_URL, decode_responses=True, - socket_connect_timeout=2, - socket_timeout=2, + socket_connect_timeout=10, + socket_timeout=10, ) return Cache(client) \ No newline at end of file diff --git a/app/main.py b/app/main.py index 58bc9f3..1f3f80e 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,7 @@ import uuid from contextlib import asynccontextmanager -from fastapi import FastAPI, Request +from fastapi import FastAPI, Request, HTTPException from fastapi.responses import JSONResponse from fastapi.exceptions import RequestValidationError @@ -55,6 +55,94 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE ) +@app.exception_handler(HTTPException) +async def http_exception_handler(request: Request, exc: HTTPException): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + + headers = {"X-Request-Id": rid} + if exc.headers: + headers.update(exc.headers) + + code_map = { + 400: "invalid_request", + 401: "unauthorized", + 404: "not_found", + 409: "conflict", + 413: "payload_too_large", + 415: "unsupported_media_type", + 429: "rate_limited", + 503: "unavailable", + } + code = code_map.get(exc.status_code, "internal_error") + + message = exc.detail if isinstance(exc.detail, str) else "error" + + return JSONResponse( + status_code=exc.status_code, + content={ + "error": { + "code": code, + "message": message, + "request_id": rid, + } + }, + headers=headers, + ) + + +# PostgreSQL: ошибки соединения → 503 +try: + from asyncpg.exceptions import ( + PostgresConnectionError, + TooManyConnectionsError, + CannotConnectNowError, + ConnectionDoesNotExistError, + ) + + @app.exception_handler(PostgresConnectionError) + @app.exception_handler(TooManyConnectionsError) + @app.exception_handler(CannotConnectNowError) + @app.exception_handler(ConnectionDoesNotExistError) + async def postgres_unavailable_handler(request: Request, exc: Exception): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "unavailable", + "message": "database unavailable", + "request_id": rid, + } + }, + headers={"X-Request-Id": rid}, + ) +except ImportError: + pass + + +# Redis: ошибки соединения → 503 +try: + import redis.exceptions as redis_exceptions + + @app.exception_handler(redis_exceptions.ConnectionError) + @app.exception_handler(redis_exceptions.TimeoutError) + async def redis_unavailable_handler(request: Request, exc: Exception): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "unavailable", + "message": "cache unavailable", + "request_id": rid, + } + }, + headers={"X-Request-Id": rid}, + ) +except ImportError: + pass + + @app.exception_handler(Exception) async def generic_exception_handler(request: Request, exc: Exception): rid = getattr(request.state, "request_id", uuid.uuid4().hex) diff --git a/app/security.py b/app/security.py index 409c985..827ace2 100644 --- a/app/security.py +++ b/app/security.py @@ -52,14 +52,28 @@ async def get_current_user( token = parse_bearer(authorization) session_store = request.app.state.session_store storage = request.app.state.storage - session = await session_store.get(token) + + try: + session = await session_store.get(token) + except Exception: + raise HTTPException(status_code=503, detail="unavailable") + if session is None: raise HTTPException(status_code=401, detail="unauthorized") + expires_at = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00")) if expires_at <= datetime.now(timezone.utc): - await session_store.delete(token) + try: + await session_store.delete(token) + except Exception: + pass raise HTTPException(status_code=401, detail="unauthorized") - user = await storage.get_user(session["user_id"]) + + try: + user = await storage.get_user(session["user_id"]) + except Exception: + raise HTTPException(status_code=503, detail="unavailable") + if user is None: raise HTTPException(status_code=401, detail="unauthorized") return user, token diff --git a/app/session_store.py b/app/session_store.py index 39df332..6fa68cf 100644 --- a/app/session_store.py +++ b/app/session_store.py @@ -101,8 +101,8 @@ def make_session_store(pool: asyncpg.Pool) -> PostgresSessionStore | RedisSessio client = redis.from_url( config.CACHE_URL, decode_responses=True, - socket_connect_timeout=2, - socket_timeout=2, + socket_connect_timeout=10, + socket_timeout=10, ) return RedisSessionStore(client) return PostgresSessionStore(pool) \ No newline at end of file diff --git a/app/storage.py b/app/storage.py index ca86d2c..659f17e 100644 --- a/app/storage.py +++ b/app/storage.py @@ -1,10 +1,11 @@ +import asyncio import uuid from datetime import datetime, timezone from typing import Optional -from app.cache import Cache import asyncpg -import asyncio + +from app.cache import Cache def _now_iso(value: datetime) -> str: @@ -118,9 +119,11 @@ class Storage: where = "" if cursor: try: - cur_created, cur_id = cursor.split("|", 1) + cur_actor, cur_created, cur_id = cursor.split("|", 2) except ValueError: raise ValueError("bad_cursor") + if cur_actor != actor_id: + raise ValueError("bad_cursor") where = "AND (c.created_at, c.id) > ($2, $3)" params += [ datetime.fromisoformat(cur_created.replace("Z", "+00:00")), @@ -152,7 +155,7 @@ class Storage: next_cursor = None if len(rows) > limit: last = rows[limit - 1] - next_cursor = f"{_now_iso(last['created_at'])}|{last['id']}" + next_cursor = f"{actor_id}|{_now_iso(last['created_at'])}|{last['id']}" return items, next_cursor async def add_message( @@ -180,8 +183,15 @@ class Storage: async def list_messages( self, chat_id: str, actor_id: str, limit: int, cursor: Optional[str], ) -> tuple[list[dict], Optional[str]]: - cache_key = f"messages:{chat_id}:{limit}:{cursor or ''}" + async with self._pool.acquire() as conn: + is_member = await conn.fetchval( + "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", + chat_id, actor_id, + ) + if is_member is None: + raise ValueError("not_found") + cache_key = f"messages:{chat_id}:{limit}:{cursor or ''}" cached = await self._cache.get_json(cache_key) if cached is not None: return cached["items"], cached["next_cursor"] @@ -201,19 +211,15 @@ class Storage: try: async with self._pool.acquire() as conn: - is_member = await conn.fetchval( - "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", - chat_id, actor_id, - ) - if is_member is None: - raise ValueError("not_found") params: list = [chat_id] where = "" if cursor: try: - cur_created, cur_id = cursor.split("|", 1) + cur_chat, cur_created, cur_id = cursor.split("|", 2) except ValueError: raise ValueError("bad_cursor") + if cur_chat != chat_id: + raise ValueError("bad_cursor") where = "AND (created_at, id) > ($2, $3)" params += [ datetime.fromisoformat(cur_created.replace("Z", "+00:00")), @@ -233,7 +239,7 @@ class Storage: next_cursor = None if len(rows) > limit: last = rows[limit - 1] - next_cursor = f"{_now_iso(last['created_at'])}|{last['id']}" + next_cursor = f"{chat_id}|{_now_iso(last['created_at'])}|{last['id']}" if got_lock: await self._cache.set_json( @@ -244,6 +250,7 @@ class Storage: finally: if got_lock: await self._cache.unlock(lock_key) + @staticmethod def _user_row(row) -> dict: return { diff --git a/evidence/cache_experiment.json b/evidence/cache_experiment.json index 7ab86cc..00d5d10 100644 --- a/evidence/cache_experiment.json +++ b/evidence/cache_experiment.json @@ -11,5 +11,31 @@ "p99": 2296.87 }, "sql_calls_from_messages": 1 + }, + { + "label": "before", + "concurrency": 50, + "elapsed_s": 6.831, + "ok": 50, + "errors": 0, + "latency_ms": { + "p50": 2042.37, + "p95": 2061.98, + "p99": 2066.18 + }, + "sql_calls_from_messages": 1 + }, + { + "label": "after", + "concurrency": 50, + "elapsed_s": 6.094, + "ok": 50, + "errors": 0, + "latency_ms": { + "p50": 2043.57, + "p95": 2072.26, + "p99": 2079.85 + }, + "sql_calls_from_messages": 1 } ] \ No newline at end of file