Add pool_pre_ping and pool_recycle to SQLAlchemy engine

Without pool_pre_ping, stale pooled connections (left over after a Postgres
restart or network blip) cause immediate query failures. SQLAlchemy then
propagates the error rather than transparently reconnecting, which crashes
the Quart request handler and triggers a Swarm restart loop.

- pool_pre_ping=True: issues a lightweight SELECT 1 before each checkout;
  discards and replaces stale connections silently
- pool_recycle=1800: recycles connections every 30 minutes to prevent
  long-idle connections from going stale at the TCP/firewall level

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-24 18:39:53 -05:00
parent f76266fa56
commit f055b361aa
+6 -1
View File
@@ -3,7 +3,12 @@ from sqlalchemy.orm import DeclarativeBase
from fabledassistant.config import Config
engine = create_async_engine(Config.DATABASE_URL, echo=False)
engine = create_async_engine(
Config.DATABASE_URL,
echo=False,
pool_pre_ping=True,
pool_recycle=1800,
)
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)