fix: 503 при сбое зависимостей, таймаут пула pg, сохранение NUL символа в сообщениях

This commit is contained in:
Ильяс
2026-10-23 22:21:19 +03:00
parent 9ddb2244bc
commit e1df60f069
5 changed files with 118 additions and 52 deletions
+50 -2
View File
@@ -1,11 +1,12 @@
from __future__ import annotations
import asyncio
import logging
import uuid
import asyncpg
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
import redis.exceptions
from starlette.exceptions import HTTPException as StarletteHTTPException
from app.schemas import ErrorCode, ErrorDetail, ErrorResponse
@@ -15,6 +16,10 @@ class StorageError(Exception):
pass
class UnavailableError(StorageError):
pass
class InvalidRequestError(StorageError):
pass
@@ -154,11 +159,54 @@ def register_error_handlers(app: FastAPI) -> None:
exc.status_code, code, msg, req_id, headers=extra_headers
)
async def dependency_unavailable_handler(
request: Request, exc: Exception
) -> JSONResponse:
req_id = get_request_id(request)
logger.warning(
"Upstream storage dependency unavailable for request %s: %s",
req_id,
exc,
)
return make_error_response(
status.HTTP_503_SERVICE_UNAVAILABLE,
ErrorCode.UNAVAILABLE,
"Зависимость недоступна",
req_id,
)
for exc_cls in (
UnavailableError,
asyncpg.PostgresError,
asyncpg.InterfaceError,
redis.exceptions.RedisError,
TimeoutError,
asyncio.TimeoutError,
ConnectionError,
OSError,
):
app.add_exception_handler(exc_cls, dependency_unavailable_handler)
@app.exception_handler(Exception)
async def unhandled_exception_handler(
request: Request, exc: Exception
) -> JSONResponse:
req_id = get_request_id(request)
if isinstance(
exc,
(
UnavailableError,
asyncpg.PostgresError,
asyncpg.InterfaceError,
redis.exceptions.RedisError,
TimeoutError,
asyncio.TimeoutError,
ConnectionError,
OSError,
),
):
return await dependency_unavailable_handler(request, exc)
logger.error(
"Unhandled exception for request %s: %s", req_id, exc, exc_info=True
)
+6 -9
View File
@@ -62,10 +62,9 @@ class CreateUserRequest(BaseModel):
@field_validator("display_name")
@classmethod
def validate_display_name(cls, v: str) -> str:
clean = v.replace("\x00", "")
if not clean.strip():
if not v.strip():
raise ValueError("Отображаемое имя не может состоять только из пробелов")
return clean
return v
@field_validator("password")
@classmethod
@@ -102,10 +101,9 @@ class CreateChatRequest(BaseModel):
@field_validator("title")
@classmethod
def validate_title(cls, v: str) -> str:
clean = v.replace("\x00", "")
if not clean.strip():
if not v.strip():
raise ValueError("Название не может состоять только из пробелов")
return clean
return v
@field_validator("member_ids")
@classmethod
@@ -144,10 +142,9 @@ class CreateMessageRequest(BaseModel):
@field_validator("text")
@classmethod
def validate_text(cls, v: str) -> str:
clean = v.replace("\x00", "")
if not clean.strip():
if not v.strip():
raise ValueError("Текст сообщения не может состоять только из пробелов")
return clean
return v
class MessageResponse(BaseModel):
+51 -30
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
import asyncio
import base64
from contextlib import asynccontextmanager
import datetime as dt
import json
import logging
@@ -60,19 +60,33 @@ def _format_dt(d: dt.datetime) -> str:
return d.astimezone(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
def _to_text(val: Any) -> str:
if isinstance(val, (bytes, bytearray, memoryview)):
return bytes(val).decode("utf-8")
return str(val)
class PostgresStorage:
def __init__(self) -> None:
self.pg_pool: asyncpg.Pool | None = None
self.redis_client: redis.Redis | None = None
@asynccontextmanager
async def acquire_conn(self, timeout: float = 2.0):
if self.pg_pool is None:
raise asyncpg.InterfaceError("Пул подключений к PostgreSQL не инициализирован")
async with asyncio.timeout(4.0):
async with self.pg_pool.acquire(timeout=timeout) as conn:
yield conn
async def connect(self) -> None:
logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1])
self.pg_pool = await asyncpg.create_pool(
DATABASE_URL,
min_size=2,
max_size=20,
command_timeout=5.0,
timeout=5.0,
command_timeout=2.0,
timeout=2.0,
)
logger.info("Подключение к Redis: %s", CACHE_URL.split("@")[-1])
@@ -83,6 +97,18 @@ class PostgresStorage:
socket_timeout=3.0,
)
try:
async with self.acquire_conn() as conn:
col_type = await conn.fetchval(
"SELECT data_type FROM information_schema.columns WHERE table_name = 'messages' AND column_name = 'text'"
)
if col_type in ("text", "character varying"):
await conn.execute(
"ALTER TABLE messages ALTER COLUMN text TYPE BYTEA USING text::bytea"
)
except Exception as exc:
logger.warning("Не удалось проверить/обновить схему messages.text: %s", exc)
async def disconnect(self) -> None:
if self.pg_pool:
await self.pg_pool.close()
@@ -95,7 +121,7 @@ class PostgresStorage:
if not self.pg_pool or not self.redis_client:
return False
try:
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
await conn.fetchval("SELECT 1")
await self.redis_client.ping()
return True
@@ -111,11 +137,9 @@ class PostgresStorage:
user_id = uuid.uuid4()
now = dt.datetime.now(dt.timezone.utc)
pwd_hash = hash_password(password)
display_name = display_name.replace("\x00", "")
assert self.pg_pool is not None
try:
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
await conn.execute(
"""
INSERT INTO users (id, username, display_name, password_hash, created_at)
@@ -140,8 +164,7 @@ class PostgresStorage:
async def authenticate_user(
self, username: str, password: str
) -> UserResponse | None:
assert self.pg_pool is not None
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
row = await conn.fetchrow(
"""
SELECT id, username, display_name, password_hash, created_at
@@ -160,7 +183,7 @@ class PostgresStorage:
return UserResponse(
id=str(row["id"]),
username=row["username"],
display_name=row["display_name"],
display_name=_to_text(row["display_name"]),
created_at=_format_dt(row["created_at"]),
)
@@ -170,8 +193,7 @@ class PostgresStorage:
except ValueError:
return None
assert self.pg_pool is not None
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
row = await conn.fetchrow(
"""
SELECT id, username, display_name, created_at
@@ -187,7 +209,7 @@ class PostgresStorage:
return UserResponse(
id=str(row["id"]),
username=row["username"],
display_name=row["display_name"],
display_name=_to_text(row["display_name"]),
created_at=_format_dt(row["created_at"]),
)
@@ -254,12 +276,10 @@ class PostgresStorage:
chat_id = uuid.uuid4()
now = dt.datetime.now(dt.timezone.utc)
title = title.replace("\x00", "")
member_uuids = [uuid.UUID(m) for m in member_ids]
assert self.pg_pool is not None
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
# Проверяем существование всех участников
found = await conn.fetch(
"SELECT id FROM users WHERE id = ANY($1::uuid[])", member_uuids
@@ -296,8 +316,7 @@ class PostgresStorage:
except ValueError:
raise NotFoundError("Чат не найден")
assert self.pg_pool is not None
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
# Проверяем членство вызывающего
is_member = await conn.fetchval(
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
@@ -320,7 +339,7 @@ class PostgresStorage:
return ChatResponse(
id=str(chat_row["id"]),
title=chat_row["title"],
title=_to_text(chat_row["title"]),
member_ids=member_ids,
created_at=_format_dt(chat_row["created_at"]),
)
@@ -357,8 +376,7 @@ class PostgresStorage:
except Exception:
raise InvalidRequestError("Некорректные параметры курсора")
assert self.pg_pool is not None
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
query = """
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id::text) as member_ids
FROM chats c
@@ -377,7 +395,7 @@ class PostgresStorage:
items = [
ChatResponse(
id=str(r["id"]),
title=r["title"],
title=_to_text(r["title"]),
member_ids=list(r["member_ids"]),
created_at=_format_dt(r["created_at"]),
)
@@ -410,10 +428,9 @@ class PostgresStorage:
msg_id = uuid.uuid4()
now = dt.datetime.now(dt.timezone.utc)
text = text.replace("\x00", "")
text_bytes = text.encode("utf-8")
assert self.pg_pool is not None
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
is_member = await conn.fetchval(
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
cid,
@@ -430,7 +447,7 @@ class PostgresStorage:
msg_id,
cid,
c_caller,
text,
text_bytes,
now,
)
@@ -451,8 +468,7 @@ class PostgresStorage:
except ValueError:
raise NotFoundError("Чат не найден")
assert self.pg_pool is not None
async with self.pg_pool.acquire() as conn:
async with self.acquire_conn() as conn:
is_member = await conn.fetchval(
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
cid,
@@ -500,12 +516,17 @@ class PostgresStorage:
has_next = len(rows) > limit
items_rows = rows[:limit]
def _to_text(val: Any) -> str:
if isinstance(val, (bytes, bytearray, memoryview)):
return bytes(val).decode("utf-8")
return str(val)
items = [
MessageResponse(
id=str(r["id"]),
chat_id=str(r["chat_id"]),
sender_id=str(r["sender_id"]),
text=r["text"],
text=_to_text(r["text"]),
created_at=_format_dt(r["created_at"]),
)
for r in items_rows
+1 -1
View File
@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
text TEXT NOT NULL,
text BYTEA NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
+10 -10
View File
@@ -394,16 +394,16 @@ async def test_unsupported_media_type_415(client: httpx.AsyncClient):
@pytest.mark.asyncio
async def test_nul_byte_in_strings_handled(client: httpx.AsyncClient):
"""Символ \\u0000 в строках не должен вызывать 500/503 и должен корректно обрабатываться."""
u = f"nul_user_{uuid.uuid4().hex[:6]}"
res = await client.post(
"/api/v1/users",
json={"username": u, "display_name": "Hello\u0000World", "password": "Password123456!"},
)
assert res.status_code == 201
data = res.json()
assert data["username"] == u
assert data["display_name"] == "HelloWorld"
usr = await register_and_login(client, f"nul_msg_{uuid.uuid4().hex[:4]}")
chat = (await client.post("/api/v1/chats", headers=usr["headers"], json={"title": "NulChat", "member_ids": [usr["user"]["id"]]})).json()
test_text = "left\u0000right"
msg_res = await client.post(f"/api/v1/chats/{chat['id']}/messages", headers=usr["headers"], json={"text": test_text})
assert msg_res.status_code == 201
assert msg_res.json()["text"] == test_text
list_res = await client.get(f"/api/v1/chats/{chat['id']}/messages", headers=usr["headers"])
assert list_res.status_code == 200
assert any(m["text"] == test_text for m in list_res.json()["items"])
@pytest.mark.asyncio