v1.0.92: Steps 5-9 — dynamic services, PostgreSQL history, /api/history

This commit is contained in:
2026-07-27 22:59:52 +04:00
parent 4952f8b096
commit d95afc60e3
7 changed files with 219 additions and 9 deletions
+39
View File
@@ -0,0 +1,39 @@
"""
Connection pool для PostgreSQL (psycopg2).
Lazy-init: пул создаётся при первом обращении к БД в каждом gunicorn-воркере.
Это безопасно для fork-модели — соединения открываются ПОСЛЕ fork.
DSN: из переменной окружения DATABASE_URL.
Формат: postgresql://user:pass@host:5432/dbname?sslmode=require
"""
import os
import psycopg2
from psycopg2 import pool
_pool = None
def get_pool():
"""Lazy-init ThreadedConnectionPool (1-5 соединений)."""
global _pool
if _pool is None:
dsn = os.getenv("DATABASE_URL", "")
if not dsn:
return None
_pool = pool.ThreadedConnectionPool(1, 5, dsn)
return _pool
def get_conn():
"""Взять соединение из пула."""
p = get_pool()
return p.getconn() if p else None
def put_conn(conn):
"""Вернуть соединение в пул."""
p = get_pool()
if p and conn:
p.putconn(conn)