forked from mtusi2026/mtusi2026lab3
128 lines
3.3 KiB
Python
128 lines
3.3 KiB
Python
from contextlib import contextmanager
|
|
import contextvars
|
|
from datetime import timezone
|
|
import itertools
|
|
import logging
|
|
import os
|
|
import socket
|
|
import threading
|
|
import time
|
|
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
from psycopg_pool import ConnectionPool
|
|
|
|
from config import settings
|
|
|
|
log = logging.getLogger('messenger.db')
|
|
|
|
request_deadline = contextvars.ContextVar('request_deadline', default=None)
|
|
|
|
CHECK_SECONDS = 1
|
|
|
|
|
|
def abort(conn):
|
|
try:
|
|
sock = socket.socket(fileno=os.dup(conn.fileno()))
|
|
except (OSError, psycopg.Error):
|
|
return
|
|
try:
|
|
sock.shutdown(socket.SHUT_RDWR)
|
|
except OSError:
|
|
pass
|
|
finally:
|
|
sock.close()
|
|
|
|
|
|
class Watchdog:
|
|
def __init__(self):
|
|
self.cond = threading.Condition()
|
|
self.armed = {}
|
|
self.ids = itertools.count()
|
|
threading.Thread(target=self.loop, name='db-watchdog', daemon=True).start()
|
|
|
|
@contextmanager
|
|
def guard(self, conn, seconds):
|
|
key = next(self.ids)
|
|
with self.cond:
|
|
self.armed[key] = (time.monotonic() + seconds, conn)
|
|
self.cond.notify()
|
|
try:
|
|
yield
|
|
finally:
|
|
with self.cond:
|
|
self.armed.pop(key, None)
|
|
|
|
def loop(self):
|
|
with self.cond:
|
|
while True:
|
|
now = time.monotonic()
|
|
for key, (deadline, conn) in list(self.armed.items()):
|
|
if deadline <= now:
|
|
del self.armed[key]
|
|
log.warning('database did not answer in time, aborting connection')
|
|
abort(conn)
|
|
wake = min((deadline for deadline, _ in self.armed.values()), default=now + 60)
|
|
self.cond.wait(max(wake - now, 0.01))
|
|
|
|
|
|
watchdog = Watchdog()
|
|
|
|
|
|
def check(conn):
|
|
with watchdog.guard(conn, CHECK_SECONDS):
|
|
ConnectionPool.check_connection(conn)
|
|
|
|
|
|
pool = ConnectionPool(
|
|
settings.database_url,
|
|
min_size=settings.db_pool_min,
|
|
max_size=settings.db_pool_max,
|
|
timeout=settings.db_pool_timeout,
|
|
check=check,
|
|
open=False,
|
|
name='messenger',
|
|
kwargs={
|
|
'connect_timeout': 3,
|
|
'row_factory': dict_row,
|
|
'options': '-c timezone=UTC -c statement_timeout=%d' % settings.db_statement_timeout_ms,
|
|
},
|
|
)
|
|
|
|
|
|
def remaining(limit=None):
|
|
budget = request_deadline.get()
|
|
if budget is None:
|
|
left = settings.db_request_timeout
|
|
else:
|
|
if budget['deadline'] is None:
|
|
budget['deadline'] = time.monotonic() + settings.db_request_timeout
|
|
left = budget['deadline'] - time.monotonic()
|
|
if limit is not None:
|
|
left = min(left, limit)
|
|
if left <= 0:
|
|
raise psycopg.OperationalError('request deadline exceeded')
|
|
return left
|
|
|
|
|
|
@contextmanager
|
|
def connection(limit=None):
|
|
conn = pool.getconn(timeout=min(settings.db_pool_timeout, remaining(limit)))
|
|
try:
|
|
with watchdog.guard(conn, remaining(limit)):
|
|
try:
|
|
yield conn
|
|
except BaseException:
|
|
try:
|
|
conn.rollback()
|
|
except psycopg.Error:
|
|
pass
|
|
raise
|
|
conn.commit()
|
|
finally:
|
|
pool.putconn(conn)
|
|
|
|
|
|
def iso(value):
|
|
return value.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%fZ')
|