forked from mtusi2026/mtusi2026lab2
fix: 503 при сбое зависимостей, таймаут пула pg, сохранение NUL символа в сообщениях
This commit is contained in:
+50
-2
@@ -1,11 +1,12 @@
|
|||||||
from __future__ import annotations
|
import asyncio
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import asyncpg
|
||||||
from fastapi import FastAPI, Request, status
|
from fastapi import FastAPI, Request, status
|
||||||
from fastapi.exceptions import RequestValidationError
|
from fastapi.exceptions import RequestValidationError
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
|
import redis.exceptions
|
||||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
|
|
||||||
from app.schemas import ErrorCode, ErrorDetail, ErrorResponse
|
from app.schemas import ErrorCode, ErrorDetail, ErrorResponse
|
||||||
@@ -15,6 +16,10 @@ class StorageError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UnavailableError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class InvalidRequestError(StorageError):
|
class InvalidRequestError(StorageError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -154,11 +159,54 @@ def register_error_handlers(app: FastAPI) -> None:
|
|||||||
exc.status_code, code, msg, req_id, headers=extra_headers
|
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)
|
@app.exception_handler(Exception)
|
||||||
async def unhandled_exception_handler(
|
async def unhandled_exception_handler(
|
||||||
request: Request, exc: Exception
|
request: Request, exc: Exception
|
||||||
) -> JSONResponse:
|
) -> JSONResponse:
|
||||||
req_id = get_request_id(request)
|
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(
|
logger.error(
|
||||||
"Unhandled exception for request %s: %s", req_id, exc, exc_info=True
|
"Unhandled exception for request %s: %s", req_id, exc, exc_info=True
|
||||||
)
|
)
|
||||||
|
|||||||
+6
-9
@@ -62,10 +62,9 @@ class CreateUserRequest(BaseModel):
|
|||||||
@field_validator("display_name")
|
@field_validator("display_name")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_display_name(cls, v: str) -> str:
|
def validate_display_name(cls, v: str) -> str:
|
||||||
clean = v.replace("\x00", "")
|
if not v.strip():
|
||||||
if not clean.strip():
|
|
||||||
raise ValueError("Отображаемое имя не может состоять только из пробелов")
|
raise ValueError("Отображаемое имя не может состоять только из пробелов")
|
||||||
return clean
|
return v
|
||||||
|
|
||||||
@field_validator("password")
|
@field_validator("password")
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -102,10 +101,9 @@ class CreateChatRequest(BaseModel):
|
|||||||
@field_validator("title")
|
@field_validator("title")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_title(cls, v: str) -> str:
|
def validate_title(cls, v: str) -> str:
|
||||||
clean = v.replace("\x00", "")
|
if not v.strip():
|
||||||
if not clean.strip():
|
|
||||||
raise ValueError("Название не может состоять только из пробелов")
|
raise ValueError("Название не может состоять только из пробелов")
|
||||||
return clean
|
return v
|
||||||
|
|
||||||
@field_validator("member_ids")
|
@field_validator("member_ids")
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -144,10 +142,9 @@ class CreateMessageRequest(BaseModel):
|
|||||||
@field_validator("text")
|
@field_validator("text")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_text(cls, v: str) -> str:
|
def validate_text(cls, v: str) -> str:
|
||||||
clean = v.replace("\x00", "")
|
if not v.strip():
|
||||||
if not clean.strip():
|
|
||||||
raise ValueError("Текст сообщения не может состоять только из пробелов")
|
raise ValueError("Текст сообщения не может состоять только из пробелов")
|
||||||
return clean
|
return v
|
||||||
|
|
||||||
|
|
||||||
class MessageResponse(BaseModel):
|
class MessageResponse(BaseModel):
|
||||||
|
|||||||
+51
-30
@@ -1,6 +1,6 @@
|
|||||||
from __future__ import annotations
|
import asyncio
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import json
|
import json
|
||||||
import logging
|
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")
|
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:
|
class PostgresStorage:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.pg_pool: asyncpg.Pool | None = None
|
self.pg_pool: asyncpg.Pool | None = None
|
||||||
self.redis_client: redis.Redis | 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:
|
async def connect(self) -> None:
|
||||||
logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1])
|
logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1])
|
||||||
self.pg_pool = await asyncpg.create_pool(
|
self.pg_pool = await asyncpg.create_pool(
|
||||||
DATABASE_URL,
|
DATABASE_URL,
|
||||||
min_size=2,
|
min_size=2,
|
||||||
max_size=20,
|
max_size=20,
|
||||||
command_timeout=5.0,
|
command_timeout=2.0,
|
||||||
timeout=5.0,
|
timeout=2.0,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info("Подключение к Redis: %s", CACHE_URL.split("@")[-1])
|
logger.info("Подключение к Redis: %s", CACHE_URL.split("@")[-1])
|
||||||
@@ -83,6 +97,18 @@ class PostgresStorage:
|
|||||||
socket_timeout=3.0,
|
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:
|
async def disconnect(self) -> None:
|
||||||
if self.pg_pool:
|
if self.pg_pool:
|
||||||
await self.pg_pool.close()
|
await self.pg_pool.close()
|
||||||
@@ -95,7 +121,7 @@ class PostgresStorage:
|
|||||||
if not self.pg_pool or not self.redis_client:
|
if not self.pg_pool or not self.redis_client:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
async with self.pg_pool.acquire() as conn:
|
async with self.acquire_conn() as conn:
|
||||||
await conn.fetchval("SELECT 1")
|
await conn.fetchval("SELECT 1")
|
||||||
await self.redis_client.ping()
|
await self.redis_client.ping()
|
||||||
return True
|
return True
|
||||||
@@ -111,11 +137,9 @@ class PostgresStorage:
|
|||||||
user_id = uuid.uuid4()
|
user_id = uuid.uuid4()
|
||||||
now = dt.datetime.now(dt.timezone.utc)
|
now = dt.datetime.now(dt.timezone.utc)
|
||||||
pwd_hash = hash_password(password)
|
pwd_hash = hash_password(password)
|
||||||
display_name = display_name.replace("\x00", "")
|
|
||||||
|
|
||||||
assert self.pg_pool is not None
|
|
||||||
try:
|
try:
|
||||||
async with self.pg_pool.acquire() as conn:
|
async with self.acquire_conn() as conn:
|
||||||
await conn.execute(
|
await conn.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||||
@@ -140,8 +164,7 @@ class PostgresStorage:
|
|||||||
async def authenticate_user(
|
async def authenticate_user(
|
||||||
self, username: str, password: str
|
self, username: str, password: str
|
||||||
) -> UserResponse | None:
|
) -> UserResponse | None:
|
||||||
assert self.pg_pool is not None
|
async with self.acquire_conn() as conn:
|
||||||
async with self.pg_pool.acquire() as conn:
|
|
||||||
row = await conn.fetchrow(
|
row = await conn.fetchrow(
|
||||||
"""
|
"""
|
||||||
SELECT id, username, display_name, password_hash, created_at
|
SELECT id, username, display_name, password_hash, created_at
|
||||||
@@ -160,7 +183,7 @@ class PostgresStorage:
|
|||||||
return UserResponse(
|
return UserResponse(
|
||||||
id=str(row["id"]),
|
id=str(row["id"]),
|
||||||
username=row["username"],
|
username=row["username"],
|
||||||
display_name=row["display_name"],
|
display_name=_to_text(row["display_name"]),
|
||||||
created_at=_format_dt(row["created_at"]),
|
created_at=_format_dt(row["created_at"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -170,8 +193,7 @@ class PostgresStorage:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
assert self.pg_pool is not None
|
async with self.acquire_conn() as conn:
|
||||||
async with self.pg_pool.acquire() as conn:
|
|
||||||
row = await conn.fetchrow(
|
row = await conn.fetchrow(
|
||||||
"""
|
"""
|
||||||
SELECT id, username, display_name, created_at
|
SELECT id, username, display_name, created_at
|
||||||
@@ -187,7 +209,7 @@ class PostgresStorage:
|
|||||||
return UserResponse(
|
return UserResponse(
|
||||||
id=str(row["id"]),
|
id=str(row["id"]),
|
||||||
username=row["username"],
|
username=row["username"],
|
||||||
display_name=row["display_name"],
|
display_name=_to_text(row["display_name"]),
|
||||||
created_at=_format_dt(row["created_at"]),
|
created_at=_format_dt(row["created_at"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -254,12 +276,10 @@ class PostgresStorage:
|
|||||||
|
|
||||||
chat_id = uuid.uuid4()
|
chat_id = uuid.uuid4()
|
||||||
now = dt.datetime.now(dt.timezone.utc)
|
now = dt.datetime.now(dt.timezone.utc)
|
||||||
title = title.replace("\x00", "")
|
|
||||||
|
|
||||||
member_uuids = [uuid.UUID(m) for m in member_ids]
|
member_uuids = [uuid.UUID(m) for m in member_ids]
|
||||||
|
|
||||||
assert self.pg_pool is not None
|
async with self.acquire_conn() as conn:
|
||||||
async with self.pg_pool.acquire() as conn:
|
|
||||||
# Проверяем существование всех участников
|
# Проверяем существование всех участников
|
||||||
found = await conn.fetch(
|
found = await conn.fetch(
|
||||||
"SELECT id FROM users WHERE id = ANY($1::uuid[])", member_uuids
|
"SELECT id FROM users WHERE id = ANY($1::uuid[])", member_uuids
|
||||||
@@ -296,8 +316,7 @@ class PostgresStorage:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
raise NotFoundError("Чат не найден")
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
assert self.pg_pool is not None
|
async with self.acquire_conn() as conn:
|
||||||
async with self.pg_pool.acquire() as conn:
|
|
||||||
# Проверяем членство вызывающего
|
# Проверяем членство вызывающего
|
||||||
is_member = await conn.fetchval(
|
is_member = await conn.fetchval(
|
||||||
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
@@ -320,7 +339,7 @@ class PostgresStorage:
|
|||||||
|
|
||||||
return ChatResponse(
|
return ChatResponse(
|
||||||
id=str(chat_row["id"]),
|
id=str(chat_row["id"]),
|
||||||
title=chat_row["title"],
|
title=_to_text(chat_row["title"]),
|
||||||
member_ids=member_ids,
|
member_ids=member_ids,
|
||||||
created_at=_format_dt(chat_row["created_at"]),
|
created_at=_format_dt(chat_row["created_at"]),
|
||||||
)
|
)
|
||||||
@@ -357,8 +376,7 @@ class PostgresStorage:
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise InvalidRequestError("Некорректные параметры курсора")
|
raise InvalidRequestError("Некорректные параметры курсора")
|
||||||
|
|
||||||
assert self.pg_pool is not None
|
async with self.acquire_conn() as conn:
|
||||||
async with self.pg_pool.acquire() as conn:
|
|
||||||
query = """
|
query = """
|
||||||
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id::text) as member_ids
|
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id::text) as member_ids
|
||||||
FROM chats c
|
FROM chats c
|
||||||
@@ -377,7 +395,7 @@ class PostgresStorage:
|
|||||||
items = [
|
items = [
|
||||||
ChatResponse(
|
ChatResponse(
|
||||||
id=str(r["id"]),
|
id=str(r["id"]),
|
||||||
title=r["title"],
|
title=_to_text(r["title"]),
|
||||||
member_ids=list(r["member_ids"]),
|
member_ids=list(r["member_ids"]),
|
||||||
created_at=_format_dt(r["created_at"]),
|
created_at=_format_dt(r["created_at"]),
|
||||||
)
|
)
|
||||||
@@ -410,10 +428,9 @@ class PostgresStorage:
|
|||||||
|
|
||||||
msg_id = uuid.uuid4()
|
msg_id = uuid.uuid4()
|
||||||
now = dt.datetime.now(dt.timezone.utc)
|
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.acquire_conn() as conn:
|
||||||
async with self.pg_pool.acquire() as conn:
|
|
||||||
is_member = await conn.fetchval(
|
is_member = await conn.fetchval(
|
||||||
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
cid,
|
cid,
|
||||||
@@ -430,7 +447,7 @@ class PostgresStorage:
|
|||||||
msg_id,
|
msg_id,
|
||||||
cid,
|
cid,
|
||||||
c_caller,
|
c_caller,
|
||||||
text,
|
text_bytes,
|
||||||
now,
|
now,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -451,8 +468,7 @@ class PostgresStorage:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
raise NotFoundError("Чат не найден")
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
assert self.pg_pool is not None
|
async with self.acquire_conn() as conn:
|
||||||
async with self.pg_pool.acquire() as conn:
|
|
||||||
is_member = await conn.fetchval(
|
is_member = await conn.fetchval(
|
||||||
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
cid,
|
cid,
|
||||||
@@ -500,12 +516,17 @@ class PostgresStorage:
|
|||||||
has_next = len(rows) > limit
|
has_next = len(rows) > limit
|
||||||
items_rows = 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 = [
|
items = [
|
||||||
MessageResponse(
|
MessageResponse(
|
||||||
id=str(r["id"]),
|
id=str(r["id"]),
|
||||||
chat_id=str(r["chat_id"]),
|
chat_id=str(r["chat_id"]),
|
||||||
sender_id=str(r["sender_id"]),
|
sender_id=str(r["sender_id"]),
|
||||||
text=r["text"],
|
text=_to_text(r["text"]),
|
||||||
created_at=_format_dt(r["created_at"]),
|
created_at=_format_dt(r["created_at"]),
|
||||||
)
|
)
|
||||||
for r in items_rows
|
for r in items_rows
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS messages (
|
|||||||
id UUID PRIMARY KEY,
|
id UUID PRIMARY KEY,
|
||||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
sender_id UUID NOT NULL REFERENCES users(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
|
created_at TIMESTAMPTZ NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
+10
-10
@@ -394,16 +394,16 @@ async def test_unsupported_media_type_415(client: httpx.AsyncClient):
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_nul_byte_in_strings_handled(client: httpx.AsyncClient):
|
async def test_nul_byte_in_strings_handled(client: httpx.AsyncClient):
|
||||||
"""Символ \\u0000 в строках не должен вызывать 500/503 и должен корректно обрабатываться."""
|
usr = await register_and_login(client, f"nul_msg_{uuid.uuid4().hex[:4]}")
|
||||||
u = f"nul_user_{uuid.uuid4().hex[:6]}"
|
chat = (await client.post("/api/v1/chats", headers=usr["headers"], json={"title": "NulChat", "member_ids": [usr["user"]["id"]]})).json()
|
||||||
res = await client.post(
|
test_text = "left\u0000right"
|
||||||
"/api/v1/users",
|
msg_res = await client.post(f"/api/v1/chats/{chat['id']}/messages", headers=usr["headers"], json={"text": test_text})
|
||||||
json={"username": u, "display_name": "Hello\u0000World", "password": "Password123456!"},
|
assert msg_res.status_code == 201
|
||||||
)
|
assert msg_res.json()["text"] == test_text
|
||||||
assert res.status_code == 201
|
|
||||||
data = res.json()
|
list_res = await client.get(f"/api/v1/chats/{chat['id']}/messages", headers=usr["headers"])
|
||||||
assert data["username"] == u
|
assert list_res.status_code == 200
|
||||||
assert data["display_name"] == "HelloWorld"
|
assert any(m["text"] == test_text for m in list_res.json()["items"])
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|||||||
Reference in New Issue
Block a user