forked from mtusi2026/mtusi2026lab2
Исправления после проверки: кэш, cursor, обработка ошибок
This commit is contained in:
+20
-1
@@ -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
|
||||
|
||||
+3
-1
@@ -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",
|
||||
|
||||
+2
-2
@@ -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)
|
||||
+89
-1
@@ -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)
|
||||
|
||||
+17
-3
@@ -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
|
||||
|
||||
@@ -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)
|
||||
+20
-13
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user