v1.0.92: Steps 5-9 — dynamic services, PostgreSQL history, /api/history
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user