Files
mtusi2026lab2/src/db.py
T
vikaandCursor c4c7a6aca2 Перенести мессенджер в PostgreSQL и сессии в Redis.
Подтверждённые сообщения и действующие сессии переживают перезапуск, история читается по индексу, копия восстанавливается в новый том.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:24:59 +03:00

460 lines
14 KiB
Python

import os
import threading
import uuid
from datetime import datetime, timedelta, timezone
from pathlib import Path
import psycopg
from psycopg.rows import dict_row
from psycopg_pool import ConnectionPool, PoolTimeout
import cursor
from errors import DependencyDown
from timefmt import format_z, parse_z
# Serialises only the message cap. Registration stays unlocked so the unique index decides the race.
MESSAGE_LOCK = 814202
def _uuid(value):
if isinstance(value, uuid.UUID):
return value
return uuid.UUID(str(value))
POOL_MIN = 1
POOL_MAX = 8
POOL_TIMEOUT_SECONDS = 5
CONNECT_TIMEOUT_SECONDS = 3
STATEMENT_TIMEOUT_MS = 3000
LOCK_TIMEOUT_MS = 2000
pool = None
max_messages = 100000
_clock_lock = threading.Lock()
_last_stamp = None
def database_url():
file_path = os.environ.get('DATABASE_URL_FILE')
if file_path:
return Path(file_path).read_text(encoding='utf-8').strip()
url = os.environ.get('DATABASE_URL', '').strip()
if not url:
raise RuntimeError('DATABASE_URL is not set')
return url
def migrations_dir():
override = os.environ.get('MIGRATIONS_DIR')
if override:
return Path(override)
return Path(__file__).resolve().parents[1] / 'migrations'
def _statements(sql):
lines = []
for line in sql.splitlines():
if line.strip().startswith('--'):
continue
lines.append(line)
parts = []
for part in '\n'.join(lines).split(';'):
statement = part.strip()
if statement:
parts.append(statement)
return parts
def apply_migrations(conninfo=None):
"""Apply pending SQL files. A second call inserts nothing and returns []."""
conninfo = conninfo or database_url()
applied = []
with psycopg.connect(conninfo, connect_timeout=CONNECT_TIMEOUT_SECONDS, row_factory=dict_row) as conn:
conn.execute(
"""
CREATE TABLE IF NOT EXISTS schema_migrations (
version text PRIMARY KEY,
applied_at timestamptz NOT NULL DEFAULT now()
)
"""
)
conn.commit()
done = {row['version'] for row in conn.execute('SELECT version FROM schema_migrations')}
for path in sorted(migrations_dir().glob('*.sql')):
if path.name in done:
continue
sql = path.read_text(encoding='utf-8')
with conn.transaction():
for statement in _statements(sql):
conn.execute(statement)
conn.execute(
'INSERT INTO schema_migrations (version) VALUES (%s)',
(path.name,),
)
applied.append(path.name)
return applied
def open_pool():
global pool
if pool is not None:
return pool
options = '-c statement_timeout=%s -c lock_timeout=%s -c timezone=UTC -c application_name=messenger' % (
STATEMENT_TIMEOUT_MS,
LOCK_TIMEOUT_MS,
)
pool = ConnectionPool(
database_url(),
min_size=POOL_MIN,
max_size=POOL_MAX,
timeout=POOL_TIMEOUT_SECONDS,
max_waiting=32,
reconnect_timeout=5,
check=ConnectionPool.check_connection,
kwargs={
'connect_timeout': CONNECT_TIMEOUT_SECONDS,
'row_factory': dict_row,
'application_name': 'messenger',
'options': options,
},
open=False,
)
pool.open(wait=True, timeout=10)
return pool
def close_pool():
global pool
if pool is not None:
pool.close()
pool = None
def configure(message_cap):
global max_messages
max_messages = int(message_cap)
def _guard(fn):
try:
return fn()
except (psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout) as exc:
raise DependencyDown(type(exc).__name__) from None
def ping():
def run():
with pool.connection() as conn:
with conn.transaction():
conn.execute('SELECT 1')
_guard(run)
def stamp():
"""Wall clock, but never goes backwards inside this process.
Two inserts in the same microsecond would make the page order depend on
UUID byte order. Bumping one microsecond keeps (created_at, id) stable.
"""
global _last_stamp
with _clock_lock:
current = datetime.now(timezone.utc)
if _last_stamp is not None and current <= _last_stamp:
current = _last_stamp + timedelta(microseconds=1)
_last_stamp = current
return current
def _user_out(row):
return {
'id': str(row['id']),
'username': row['username'],
'display_name': row['display_name'],
'created_at': format_z(row['created_at']),
}
def _message_out(row):
return {
'id': str(row['id']),
'chat_id': str(row['chat_id']),
'sender_id': str(row['sender_id']),
'text': row['body'],
'created_at': format_z(row['created_at']),
}
def create_user(username, display_name, password_hash):
user_id = str(uuid.uuid4())
created = stamp()
def run():
try:
with pool.connection() as conn:
with conn.transaction():
row = conn.execute(
"""
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES (%s, %s, %s, %s, %s)
RETURNING id, username, display_name, created_at
""",
(_uuid(user_id), username, display_name, password_hash, created),
).fetchone()
return 'ok', _user_out(row)
except psycopg.errors.UniqueViolation:
return 'conflict', None
except psycopg.errors.CheckViolation:
return 'bad', None
return _guard(run)
def get_user(user_id):
def run():
with pool.connection() as conn:
with conn.transaction():
row = conn.execute(
"""
SELECT id, username, display_name, created_at
FROM users
WHERE id = %s
""",
(_uuid(user_id),),
).fetchone()
return None if row is None else _user_out(row)
return _guard(run)
def get_credentials(username):
def run():
with pool.connection() as conn:
with conn.transaction():
row = conn.execute(
"""
SELECT id, username, display_name, created_at, password_hash
FROM users
WHERE username = %s
""",
(username,),
).fetchone()
if row is None:
return None
return _user_out(row), row['password_hash']
return _guard(run)
def _members(conn, chat_ids):
if not chat_ids:
return {}
rows = conn.execute(
"""
SELECT chat_id, user_id
FROM chat_members
WHERE chat_id = ANY(%s)
ORDER BY chat_id, member_ord
""",
([_uuid(item) for item in chat_ids],),
).fetchall()
grouped = {}
for row in rows:
grouped.setdefault(str(row['chat_id']), []).append(str(row['user_id']))
return grouped
def _chat_out(row, member_ids):
return {
'id': str(row['id']),
'title': row['title'],
'member_ids': member_ids,
'created_at': format_z(row['created_at']),
}
def create_chat(owner_id, title, member_ids):
chat_id = str(uuid.uuid4())
created = stamp()
def run():
try:
with pool.connection() as conn:
with conn.transaction():
conn.execute(
"""
INSERT INTO chats (id, title, created_at)
VALUES (%s, %s, %s)
""",
(_uuid(chat_id), title, created),
)
for ordinal, member_id in enumerate(member_ids):
conn.execute(
"""
INSERT INTO chat_members (chat_id, user_id, member_ord)
VALUES (%s, %s, %s)
""",
(_uuid(chat_id), _uuid(member_id), ordinal),
)
row = conn.execute(
"""
SELECT id, title, created_at
FROM chats
WHERE id = %s
""",
(_uuid(chat_id),),
).fetchone()
members = _members(conn, [chat_id]).get(chat_id, [])
return 'ok', _chat_out(row, members)
except (psycopg.errors.ForeignKeyViolation, psycopg.errors.UniqueViolation, psycopg.errors.CheckViolation):
return 'bad', None
return _guard(run)
def get_chat(chat_id, user_id):
def run():
with pool.connection() as conn:
with conn.transaction():
row = conn.execute(
"""
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s
WHERE c.id = %s
""",
(_uuid(user_id), _uuid(chat_id)),
).fetchone()
if row is None:
return None
members = _members(conn, [chat_id]).get(chat_id, [])
return _chat_out(row, members)
return _guard(run)
def list_chats(user_id, limit, cursor_pair):
def run():
with pool.connection() as conn:
with conn.transaction():
if cursor_pair is None:
rows = conn.execute(
"""
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s
ORDER BY c.created_at, c.id
LIMIT %s
""",
(_uuid(user_id), limit + 1),
).fetchall()
else:
rows = conn.execute(
"""
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s
WHERE (c.created_at, c.id) > (%s, %s)
ORDER BY c.created_at, c.id
LIMIT %s
""",
(_uuid(user_id), parse_z(cursor_pair[0]), _uuid(cursor_pair[1]), limit + 1),
).fetchall()
members = _members(conn, [row['id'] for row in rows[:limit]])
extra = len(rows) > limit
page = rows[:limit]
items = [_chat_out(row, members.get(str(row['id']), [])) for row in page]
if extra and items:
last = items[-1]
nxt = cursor.cursor_token('chats', user_id, last['created_at'], last['id'])
else:
nxt = None
return items, nxt
return _guard(run)
def add_message(chat_id, sender_id, text):
message_id = str(uuid.uuid4())
created = stamp()
def run():
with pool.connection() as conn:
with conn.transaction():
conn.execute('SELECT pg_advisory_xact_lock(%s)', (MESSAGE_LOCK,))
count = conn.execute('SELECT count(*) AS n FROM messages').fetchone()['n']
if count >= max_messages:
return 'full', None
row = conn.execute(
"""
INSERT INTO messages (id, chat_id, sender_id, body, created_at)
SELECT %s, %s, %s, %s, %s
WHERE EXISTS (
SELECT 1 FROM chat_members
WHERE chat_id = %s AND user_id = %s
)
RETURNING id, chat_id, sender_id, body, created_at
""",
(
_uuid(message_id),
_uuid(chat_id),
_uuid(sender_id),
text,
created,
_uuid(chat_id),
_uuid(sender_id),
),
).fetchone()
if row is None:
return 'missing', None
return 'ok', _message_out(row)
return _guard(run)
def list_messages(chat_id, user_id, limit, cursor_pair):
def run():
with pool.connection() as conn:
with conn.transaction():
member = conn.execute(
"""
SELECT 1 AS ok
FROM chat_members
WHERE user_id = %s AND chat_id = %s
""",
(_uuid(user_id), _uuid(chat_id)),
).fetchone()
if member is None:
return 'missing', None, None
if cursor_pair is None:
rows = conn.execute(
"""
SELECT id, chat_id, sender_id, body, created_at
FROM messages
WHERE chat_id = %s
ORDER BY created_at, id
LIMIT %s
""",
(_uuid(chat_id), limit + 1),
).fetchall()
else:
rows = conn.execute(
"""
SELECT id, chat_id, sender_id, body, created_at
FROM messages
WHERE chat_id = %s
AND (created_at, id) > (%s, %s)
ORDER BY created_at, id
LIMIT %s
""",
(_uuid(chat_id), parse_z(cursor_pair[0]), _uuid(cursor_pair[1]), limit + 1),
).fetchall()
extra = len(rows) > limit
page = [_message_out(row) for row in rows[:limit]]
if extra and page:
last = page[-1]
nxt = cursor.cursor_token('msgs', chat_id, last['created_at'], last['id'])
else:
nxt = None
return 'ok', page, nxt
return _guard(run)