-
Notifications
You must be signed in to change notification settings - Fork 1
Replace NullPool with QueuePool and consolidate database engines #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||||
| import os | ||||||||
| from collections.abc import Generator | ||||||||
|
|
||||||||
| from sqlalchemy import NullPool, create_engine | ||||||||
| from sqlalchemy import create_engine, select | ||||||||
| from sqlalchemy.orm import Session, sessionmaker | ||||||||
|
|
||||||||
| DB_USERNAME = os.environ.get("DB_USERNAME", "postgres") | ||||||||
|
|
@@ -11,10 +11,14 @@ | |||||||
|
|
||||||||
| ENGINE = create_engine( | ||||||||
| DB_URL, | ||||||||
| poolclass=NullPool, | ||||||||
| pool_size=5, | ||||||||
| max_overflow=10, | ||||||||
| pool_pre_ping=True, | ||||||||
| pool_recycle=300, | ||||||||
| ) | ||||||||
|
|
||||||||
| SessionLocal = sessionmaker(bind=ENGINE, autoflush=False, autocommit=False) | ||||||||
| SESSION = sessionmaker(ENGINE) | ||||||||
|
||||||||
| SESSION = sessionmaker(ENGINE) | |
| SESSION = SessionLocal |
Copilot
AI
Mar 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensure_db_connection uses with db as session: on a Session instance that is typically owned/closed by the caller (e.g., FastAPI dependency). Entering the context manager will close the passed-in session on exit, which is surprising and can cause the caller to operate on a closed session (and also results in double-close with get_db_session()). Prefer executing directly on the provided Session without wrapping it in a context manager, or have this helper create/own its own Session instead.
| with db as session: | |
| session.execute(select(1)) | |
| db.execute(select(1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The connection pool sizing/tuning is hard-coded (pool_size/max_overflow/pool_recycle). This can cause unexpected connection counts in production (e.g., per-worker pools) and makes it hard to tune without a redeploy. Consider sourcing these values from environment variables (with sensible defaults) so deployments can adjust based on DB limits and worker count.