fix: единый singleton engine в bd, убраны 17 локальных make_engine()
- bd/__init__.py: добавлены get_engine() и get_session() — один SQLAlchemy engine на весь процесс - make_engine(): pool_size=5, max_overflow=5, pool_timeout=30, pool_recycle=1800 - Все 17 route-файлов: убраны локальные get_session()/make_engine(), импорт из bd - transfer_crud.py: _get_session() → get_session() из bd - init_data_base.py: get_engine() вместо make_engine() - Результат: 78 idle соединений → 1
This commit is contained in:
@@ -24,9 +24,7 @@ from pathlib import Path
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from bd import make_engine
|
||||
from bd import get_session
|
||||
from bd.tables.organization import Organization
|
||||
from bd.tables.group import Group
|
||||
from bd.tables.poll import Poll
|
||||
@@ -61,8 +59,6 @@ _TABLES: list[tuple[str, object]] = [
|
||||
]
|
||||
|
||||
|
||||
def _get_session():
|
||||
return sessionmaker(bind=make_engine(), future=True)
|
||||
|
||||
|
||||
def _row_to_dict(row) -> dict:
|
||||
@@ -89,7 +85,7 @@ def _row_to_dict(row) -> dict:
|
||||
dependencies=[Depends(require_admin_key)],
|
||||
)
|
||||
def export_data():
|
||||
Session = _get_session()
|
||||
Session = get_session()
|
||||
buf = io.BytesIO()
|
||||
counts = {}
|
||||
|
||||
@@ -147,7 +143,7 @@ def import_data(file: UploadFile = File(...)):
|
||||
if fmt not in (EXPORT_FORMAT_VERSION, "1.0"):
|
||||
raise HTTPException(status_code=400, detail=f"Неподдерживаемая версия формата: {fmt}")
|
||||
|
||||
Session = _get_session()
|
||||
Session = get_session()
|
||||
result = {"inserted": {}, "skipped": {}}
|
||||
|
||||
with Session() as session:
|
||||
@@ -218,7 +214,7 @@ backup_router = APIRouter(prefix="/backup", tags=["backup"])
|
||||
)
|
||||
def backup_export():
|
||||
"""Идентично /transfer/export, но в имени файла указан префикс backup_."""
|
||||
Session = _get_session()
|
||||
Session = get_session()
|
||||
buf = io.BytesIO()
|
||||
counts = {}
|
||||
|
||||
@@ -279,7 +275,7 @@ def backup_restore(file: UploadFile = File(...)):
|
||||
if fmt not in (EXPORT_FORMAT_VERSION, "1.0"):
|
||||
raise HTTPException(status_code=400, detail=f"Неподдерживаемая версия формата: {fmt}")
|
||||
|
||||
Session = _get_session()
|
||||
Session = get_session()
|
||||
deleted_counts: dict[str, int] = {}
|
||||
inserted_counts: dict[str, int] = {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user