forked from mtusi2026/mtusi2026lab3
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
import time
|
|
from contextlib import contextmanager
|
|
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
from psycopg_pool import ConnectionPool, PoolTimeout
|
|
|
|
from src.errors import DomainError
|
|
|
|
|
|
class DeadlineConnection(psycopg.Connection):
|
|
"""Клиентский срок действует и при замороженном PostgreSQL."""
|
|
|
|
def wait(self, gen, interval=0.1, timeout=None):
|
|
deadline = getattr(self, "request_deadline", None)
|
|
remaining = (
|
|
max(0.001, deadline - time.monotonic()) if deadline else 3.0
|
|
)
|
|
bound = min(remaining, timeout) if timeout is not None else remaining
|
|
try:
|
|
return super().wait(gen, interval=interval, timeout=bound)
|
|
except psycopg.OperationalError:
|
|
self.close()
|
|
raise
|
|
|
|
|
|
class Database:
|
|
def __init__(self, *, url: str, size: int):
|
|
self.pool = ConnectionPool(
|
|
url,
|
|
connection_class=DeadlineConnection,
|
|
kwargs={
|
|
"autocommit": True,
|
|
"row_factory": dict_row,
|
|
"connect_timeout": 2,
|
|
"options": (
|
|
"-c statement_timeout=2500 -c lock_timeout=1000 "
|
|
"-c idle_in_transaction_session_timeout=6000"
|
|
),
|
|
},
|
|
min_size=1,
|
|
max_size=size,
|
|
timeout=1,
|
|
open=False,
|
|
)
|
|
|
|
def open(self):
|
|
self.pool.open(wait=True, timeout=10)
|
|
|
|
def close(self):
|
|
self.pool.close(timeout=2)
|
|
|
|
@contextmanager
|
|
def connection(self):
|
|
try:
|
|
with self.pool.connection(timeout=1) as conn:
|
|
conn.request_deadline = time.monotonic() + 4
|
|
try:
|
|
yield conn
|
|
finally:
|
|
conn.request_deadline = None
|
|
except psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout:
|
|
raise DomainError(
|
|
503, "unavailable", "Database unavailable"
|
|
) from None
|
|
|
|
def ping(self):
|
|
with self.connection() as conn:
|
|
conn.execute("SELECT 1").fetchone()
|