mirror of
https://github.com/yogeshojha/rengine.git
synced 2026-10-20 15:21:30 +02:00
The comment rule in CLAUDE.md is one line maximum, never narration: no multi-line blocks, no module-header explainers, no sentences that explain or justify the code to the reader. The tree had drifted a long way from it, and most of the drift is mine. Removed: 232 lines of multi-line comment blocks 203 single-line comments that argued instead of stating 154 comments joined by so/because/otherwise/rather-than 143 narrating comments in the frontend 112 test docstrings (the test name already says what it asserts) 25 test module docstrings and 25 decorative section dividers Collapsed: 324 multi-line docstrings to one factual line 271 one-line docstrings that had a justifying clause bolted on Kept: functional directives, section dividers, short field labels, alembic revision headers, and the eight FastAPI route docstrings that render in /docs. Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs. 284 python tests and 34 frontend tests pass, ruff and ruff-format clean across ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
from sqlalchemy import text
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
from app.config import settings
|
|
from shared.logging import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
_server_settings = {"application_name": f"{settings.APP_NAME}-api"}
|
|
|
|
if settings.DB_IDLE_TX_TIMEOUT > 0:
|
|
_server_settings["idle_in_transaction_session_timeout"] = str(
|
|
settings.DB_IDLE_TX_TIMEOUT * 1000
|
|
)
|
|
|
|
engine = create_async_engine(
|
|
settings.database_url,
|
|
echo=settings.DEBUG,
|
|
future=True,
|
|
pool_size=settings.DB_POOL_SIZE,
|
|
max_overflow=settings.DB_MAX_OVERFLOW,
|
|
pool_timeout=settings.DB_POOL_TIMEOUT,
|
|
pool_recycle=settings.DB_POOL_RECYCLE,
|
|
pool_pre_ping=True,
|
|
connect_args={"server_settings": _server_settings},
|
|
)
|
|
|
|
async_db_session = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
)
|
|
|
|
|
|
async def get_session() -> AsyncSession:
|
|
async with async_db_session() as session:
|
|
yield session
|
|
|
|
|
|
def pool_stats() -> dict[str, int]:
|
|
pool = engine.pool
|
|
return {
|
|
"size": pool.size(),
|
|
"checked_out": pool.checkedout(),
|
|
"checked_in": pool.checkedin(),
|
|
"overflow": pool.overflow(),
|
|
"capacity": settings.DB_POOL_SIZE + settings.DB_MAX_OVERFLOW,
|
|
}
|
|
|
|
|
|
def pool_demand() -> int:
|
|
"""Connections every pool can ask for at once."""
|
|
per_child = settings.WORKER_DB_POOL_SIZE + settings.WORKER_DB_MAX_OVERFLOW
|
|
return (
|
|
settings.DB_POOL_SIZE
|
|
+ settings.DB_MAX_OVERFLOW
|
|
+ settings.CELERY_SCAN_CONCURRENCY * per_child
|
|
+ per_child
|
|
)
|
|
|
|
|
|
async def check_capacity() -> None:
|
|
"""Say out loud when the pools can outgrow the server."""
|
|
demand = pool_demand()
|
|
try:
|
|
async with engine.connect() as conn:
|
|
allowed = int(await conn.scalar(text("SHOW max_connections")))
|
|
reserved = int(
|
|
await conn.scalar(text("SHOW superuser_reserved_connections"))
|
|
)
|
|
except Exception:
|
|
logger.debug("could not read the connection ceiling", exc_info=True)
|
|
return
|
|
usable = allowed - reserved
|
|
if demand > usable:
|
|
logger.warning(
|
|
"database pools can outgrow the server",
|
|
demand=demand,
|
|
usable=usable,
|
|
hint="raise POSTGRES_MAX_CONNECTIONS or lower the pool sizes",
|
|
)
|
|
else:
|
|
logger.info("database pool headroom", demand=demand, usable=usable)
|