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
+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