Перенести приложение мессенджера с сессиями и идемпотентностью в PostgreSQL

Пользователи, аутентификация Argon2id, Bearer-сессии с TTL и отзывом,
транзакционный репозиторий, keyset-пагинация, идемпотентные отправки
(advisory lock + message_requests), версияемые миграции 001/002.
This commit is contained in:
2026-11-17 03:37:07 +00:00
parent ec540a1a72
commit 4b23b44eb8
15 changed files with 795 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
"""Short-lived database connections, one transaction per operation."""
from contextlib import contextmanager
import psycopg
from psycopg.rows import dict_row
from .domain import ApiError
class Database:
def __init__(self, url):
self.url = url
@contextmanager
def transaction(self):
try:
with psycopg.connect(self.url, row_factory=dict_row, connect_timeout=3,
options='-c statement_timeout=5000 -c lock_timeout=3000 -c timezone=UTC') as conn:
yield conn
except (psycopg.OperationalError, psycopg.InterfaceError,
psycopg.errors.QueryCanceled, psycopg.errors.LockNotAvailable):
raise ApiError(503, 'unavailable', 'Database temporarily unavailable') from None