Files
mtusi2026lab4-fork/src/storage.py
T

277 lines
14 KiB
Python

"""PostgreSQL — источник истины; каждый контекст подключения завершает транзакцию."""
import base64
import binascii
from datetime import datetime, timezone
import hashlib
import json
import os
import secrets
import threading
import uuid
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError, VerificationError, InvalidHashError
from psycopg.errors import UniqueViolation, ForeignKeyViolation
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool
from src.config import setting
from src.cache import profile_cache
from src.models import User, Chat, Message
_CURSOR_FIELDS = {"scope", "resource_id", "created_at", "id"}
PASSWORDS = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
DUMMY_HASH = PASSWORDS.hash('synthetic-dummy-password')
class ValidationError(Exception):
"""Неверный запрос."""
class ConflictError(Exception):
"""Имя уже занято."""
class LimitExceeded(Exception):
"""Перегрузка сервера."""
class NotFoundError(Exception):
"""Объект отсутствует или недоступен."""
def timestamp(value):
return value.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z')
def public_row(row):
return {k: timestamp(v) if isinstance(v, datetime) else str(v) if isinstance(v, uuid.UUID) else v
for k, v in row.items() if k != 'password_hash'}
class PostgresStorage:
def __init__(self):
self.pool = ConnectionPool(setting('DATABASE_URL', ''), min_size=1, max_size=int(os.getenv("POOL_SIZE", "10")),
timeout=3, max_waiting=32, open=False, check=ConnectionPool.check_connection,
kwargs={'row_factory': dict_row, 'connect_timeout': 3,
'options': '-c statement_timeout=3000 -c lock_timeout=2000'})
self._lock = threading.Lock()
self._requests = 0
@staticmethod
def _encode_cursor(scope: str, resource_id: str, created_at: str, item_id: str) -> str:
"""Собрать непрозрачный курсор, привязанный к конкретному ресурсу."""
payload = {
"scope": scope,
"resource_id": resource_id,
"created_at": created_at,
"id": item_id,
}
raw = json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("utf-8")
return base64.urlsafe_b64encode(raw).decode("ascii")
@staticmethod
def _decode_cursor(cursor: str, expected_scope: str, expected_resource_id: str) -> tuple[str, str]:
"""Разобрать и проверить курсор.
Клиент видит курсор как непрозрачную строку, поэтому любые пустые,
повреждённые или относящиеся к другому ресурсу значения отклоняем.
"""
if cursor == "":
raise ValidationError("некорректный cursor")
allowed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="
if any(char not in allowed_chars for char in cursor) or "=" in cursor.rstrip("="):
raise ValidationError("некорректный cursor")
try:
raw = base64.b64decode(cursor.encode("ascii"), altchars=b"-_", validate=True)
payload = json.loads(raw.decode("utf-8"))
except (UnicodeEncodeError, UnicodeDecodeError, binascii.Error, json.JSONDecodeError):
raise ValidationError("некорректный cursor")
if not isinstance(payload, dict) or set(payload) != _CURSOR_FIELDS:
raise ValidationError("некорректный cursor")
if not all(isinstance(payload[field], str) for field in _CURSOR_FIELDS):
raise ValidationError("некорректный cursor")
if payload["scope"] != expected_scope or payload["resource_id"] != expected_resource_id:
raise ValidationError("некорректный cursor")
try:
uuid.UUID(payload["resource_id"])
uuid.UUID(payload["id"])
datetime.fromisoformat(payload["created_at"].replace("Z", "+00:00"))
except ValueError:
raise ValidationError("некорректный cursor")
return payload["created_at"], payload["id"]
def create_user(self, username, display_name, password):
password_hash = PASSWORDS.hash(password)
try:
with self.pool.connection() as conn:
row = conn.execute('INSERT INTO users (id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) RETURNING id,username,display_name,created_at',
(uuid.uuid4(), username, display_name, password_hash)).fetchone()
return User(**public_row(row))
except UniqueViolation:
raise ConflictError('имя пользователя уже занято') from None
def get_user(self, user_id):
cached = profile_cache.get(user_id)
if cached:
return cached
with self.pool.connection() as conn:
row = conn.execute('SELECT id,username,display_name,created_at FROM users WHERE id=%s', (user_id,)).fetchone()
user = User(**public_row(row)) if row else None
if user:
profile_cache.put(user)
return user
def login(self, username, password):
with self.pool.connection() as conn:
row = conn.execute('SELECT * FROM users WHERE username=%s', (username,)).fetchone()
try:
PASSWORDS.verify(row['password_hash'] if row else DUMMY_HASH, password)
except (VerifyMismatchError, VerificationError, InvalidHashError):
return None
if not row:
return None
token = secrets.token_urlsafe(32)
with self.pool.connection() as conn:
session = conn.execute("INSERT INTO sessions VALUES (%s,%s,clock_timestamp() + %s * interval '1 second') RETURNING expires_at",
(self.token_hash(token), row['id'], int(os.getenv('SESSION_TTL_SECONDS', '3600')))).fetchone()
return {'token': token, 'token_type': 'Bearer', 'expires_at': timestamp(session['expires_at']), 'user': User(**public_row(row)).model_dump()}
@staticmethod
def token_hash(token):
return hashlib.sha256(token.encode()).hexdigest()
def session_user(self, token):
with self.pool.connection() as conn:
row = conn.execute('SELECT user_id FROM sessions WHERE token_hash=%s AND expires_at > clock_timestamp()', (self.token_hash(token),)).fetchone()
return str(row['user_id']) if row else None
def logout(self, token):
with self.pool.connection() as conn:
conn.execute('DELETE FROM sessions WHERE token_hash=%s', (self.token_hash(token),))
def create_chat(self, title, member_ids, creator_id):
if len(set(member_ids)) != len(member_ids) or creator_id not in member_ids:
raise ValidationError('неверный список участников')
chat_id = uuid.uuid4()
try:
with self.pool.connection() as conn:
row = conn.execute('INSERT INTO chats (id,title) VALUES (%s,%s) RETURNING *', (chat_id,title)).fetchone()
for member in member_ids:
conn.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat_id,member))
return Chat(**public_row(row), member_ids=sorted(member_ids))
except ForeignKeyViolation:
raise ValidationError('участник не существует') from None
def get_chat(self, chat_id):
with self.pool.connection() as conn:
row = conn.execute('SELECT * FROM chats WHERE id=%s', (chat_id,)).fetchone()
if not row:
return None
members = conn.execute('SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY user_id', (chat_id,)).fetchall()
return Chat(**public_row(row), member_ids=[str(x['user_id']) for x in members])
def is_chat_member(self, chat_id, user_id):
"""Проверить доступ без загрузки всего чата и списка участников."""
with self.pool.connection() as conn:
return conn.execute('SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s', (chat_id,user_id)).fetchone() is not None
def list_chats_for_user(self, user_id, cursor, limit):
with self.pool.connection() as conn:
params = [user_id]
boundary = ''
if cursor is not None:
when, item = self._decode_cursor(cursor, 'chats', user_id)
marker = conn.execute('SELECT c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE c.id=%s AND m.user_id=%s', (item,user_id)).fetchone()
if not marker or timestamp(marker['created_at']) != when:
raise ValidationError('некорректный cursor')
boundary = ' AND (c.created_at,c.id) > (%s,%s)'
params += [when,item]
rows = conn.execute('SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=%s' + boundary + ' ORDER BY c.created_at,c.id LIMIT %s', params+[limit+1]).fetchall()
page = [self.get_chat(str(row['id'])) for row in rows[:limit]]
next_cursor = self._encode_cursor('chats',user_id,page[-1].created_at,page[-1].id) if len(rows)>limit else None
return page,next_cursor
def create_message(self, chat_id, sender_id, text, key, attachment_ids=None):
attachment_ids = attachment_ids or []
normalized = {'text': text, 'attachment_ids': attachment_ids}
# Блокировка сериализует один ключ на всех репликах; результат и сообщение атомарны.
with self.pool.connection() as conn:
scope = json.dumps([sender_id, chat_id, key])
conn.execute('SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))', (scope,))
old = conn.execute('SELECT * FROM message_requests WHERE user_id=%s AND chat_id=%s AND key=%s AND expires_at>clock_timestamp()', (sender_id,chat_id,key)).fetchone()
if old:
if json.loads(old['body']) != normalized:
raise ConflictError('ключ уже использован с другим сообщением')
row = conn.execute('SELECT * FROM messages WHERE id=%s', (old['message_id'],)).fetchone()
return self.message_model(conn, row)
for attachment_id in sorted(attachment_ids):
attachment = conn.execute('SELECT state FROM attachments WHERE id=%s AND chat_id=%s FOR SHARE', (attachment_id,chat_id)).fetchone()
if not attachment:
raise NotFoundError('вложение не найдено')
if attachment['state'] != 'ready':
raise ConflictError('вложение недоступно')
optimized = os.getenv('OPTIMIZED_WRITES','1') == '1'
if not optimized:
conn.execute('DELETE FROM message_requests WHERE user_id=%s AND chat_id=%s AND key=%s', (sender_id,chat_id,key))
row = conn.execute('INSERT INTO messages (id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *', (uuid.uuid4(),chat_id,sender_id,text)).fetchone()
# Под той же блокировкой UPSERT заменяет просроченный результат без отдельного DELETE.
suffix = ' ON CONFLICT (user_id,chat_id,key) DO UPDATE SET body=EXCLUDED.body,message_id=EXCLUDED.message_id,expires_at=EXCLUDED.expires_at' if optimized else ''
conn.execute("INSERT INTO message_requests VALUES (%s,%s,%s,%s,%s,clock_timestamp()+interval '24 hours')"+suffix, (sender_id,chat_id,key,json.dumps(normalized,ensure_ascii=False),row['id']))
for position, attachment_id in enumerate(attachment_ids):
conn.execute('INSERT INTO message_attachments VALUES (%s,%s,%s)', (row['id'],attachment_id,position))
result = self.message_model(conn,row)
return result
@staticmethod
def message_model(conn, row):
ids = conn.execute('SELECT attachment_id FROM message_attachments WHERE message_id=%s ORDER BY position', (row['id'],)).fetchall()
return Message(**public_row(row), attachment_ids=[str(x['attachment_id']) for x in ids])
def list_messages(self, chat_id, cursor, limit):
with self.pool.connection() as conn:
params = [chat_id]
boundary = ''
if cursor is not None:
when,item = self._decode_cursor(cursor,'messages',chat_id)
marker = conn.execute('SELECT created_at FROM messages WHERE id=%s AND chat_id=%s', (item,chat_id)).fetchone()
if not marker or timestamp(marker['created_at']) != when:
raise ValidationError('некорректный cursor')
boundary = ' AND (created_at,id) > (%s,%s)'
params += [when,item]
rows = conn.execute('SELECT * FROM messages WHERE chat_id=%s' + boundary + ' ORDER BY created_at,id LIMIT %s', params+[limit+1]).fetchall()
page = [self.message_model(conn,row) for row in rows[:limit]]
next_cursor = self._encode_cursor('messages',chat_id,page[-1].created_at,page[-1].id) if len(rows)>limit else None
return page,next_cursor
def ready(self):
with self.pool.connection() as conn:
conn.execute('SELECT 1 FROM schema_migrations LIMIT 1')
def acquire_request_slot(self):
with self._lock:
if self._requests >= 100:
return False
self._requests += 1
return True
def release_request_slot(self):
with self._lock:
self._requests -= 1
def get_stats(self):
with self.pool.connection() as conn:
result = {table: conn.execute('SELECT count(*) AS n FROM '+table).fetchone()['n'] for table in ('users','chats','messages')}
result['profile_cache'] = {'hits': profile_cache.hits, 'misses': profile_cache.misses, 'errors': profile_cache.errors, 'ttl_seconds': profile_cache.ttl}
return result
storage = PostgresStorage()