Use NullPool for Celery workers so connections are opened/closed per task instead of accumulating in per-process pools. Add worker_process_init signal to dispose inherited engines on fork. Keep QueuePool for the web service. Increase PostgreSQL max_connections to 200 as a safety net. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
38 lines
1 KiB
Python
38 lines
1 KiB
Python
"""Database session management using SQLAlchemy."""
|
|
|
|
from collections.abc import Generator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
from sqlalchemy.pool import NullPool, QueuePool
|
|
|
|
from app.config import settings
|
|
|
|
# Select pool class based on config
|
|
pool_class = NullPool if settings.DB_POOL_CLASS == "NullPool" else QueuePool
|
|
|
|
# Build engine kwargs
|
|
engine_kwargs = {
|
|
"pool_pre_ping": True,
|
|
"poolclass": pool_class,
|
|
}
|
|
|
|
if pool_class is QueuePool:
|
|
engine_kwargs["pool_size"] = settings.DB_POOL_SIZE
|
|
engine_kwargs["max_overflow"] = settings.DB_MAX_OVERFLOW
|
|
engine_kwargs["pool_recycle"] = 1800
|
|
|
|
# Create database engine
|
|
engine = create_engine(settings.DATABASE_URL, **engine_kwargs)
|
|
|
|
# Create session factory
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
"""Dependency that yields a database session."""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|