Size the connection pool, slow the idle poll, drop a dead setting (REVIEW 7)
The three performance settings the review flagged, one of which turned out not to exist in any meaningful sense. db_pool_size 5 -> 15, and max_overflow named explicitly. FastAPI runs this app's def handlers in anyio's threadpool - 40 threads by default - and each holds a session for the whole request, so a pool of 5 put the sixth concurrent request on pool_timeout before it reached the database. The real ceiling was never 5 either: it was 15, because SQLAlchemy quietly adds max_overflow=10. Leaving that invisible is the actual hazard, since an operator raising pool_size has no way to see what they are raising it from, so both halves are settings now and the arithmetic is written down next to PostgreSQL's max_connections in the env example. pool_timeout is pinned at 30s rather than inherited: it has to stay finite, or a pool leak presents as requests hanging forever instead of an error that names the pool. worker_poll_interval_s 2.0 -> 5.0. This only ever delays noticing a job enqueued into an empty queue - the loop drains a queue that has work back to back without sleeping - so it costs latency nobody can perceive on a bird camera and halves the idle queries, 30 a minute down to 12. worker_batch_size is removed rather than raised to 5. Nothing read it. It has sat in Settings since Phase 1 looking like it tuned something while the worker claimed one job at a time regardless, which is worse than no setting at all. Implementing it would also be a mistake here: claiming five jobs marks all five 'running', so dying after the first leaves four locked by a process that no longer exists and invisible until requeue_stale reaches them up to fifteen minutes later. Single claims keep exactly one job at risk, and batching would only have saved claim round-trips on a loopback connection that no thumbnail decode would notice. The reasoning is at the claim loop, where the next person will ask. The test asserts against a pool an engine really built, because comparing Settings to itself would pass no matter what db.py does. It has to use values that differ from SQLAlchemy's own defaults - hence 17 and 7 - which is the point mutation testing earned: with max_overflow unwired the first version of this test still passed, because the default it fell back to was 10 and the setting also said 10. Tests: 193 -> 194. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ec9ef9b3a6
commit
05f144ed98
5 files changed
+83
-4
No files matched your search
@@ -37,7 +37,14 @@ class Settings(BaseSettings):
|
||||
# reported by ``poorbird check`` and raised with a readable message by db.py,
|
||||
# rather than silently connecting to whatever happens to be on localhost.
|
||||
database_url: str = ""
|
||||
db_pool_size: int = 5
|
||||
# FastAPI runs this app's ``def`` handlers in anyio's threadpool (40 threads
|
||||
# by default), and each one holds a session for the whole request. A pool of
|
||||
# 5 meant the 6th concurrent request waited on ``pool_timeout`` before it
|
||||
# even reached the database (REVIEW §7). The real ceiling is
|
||||
# pool_size + db_max_overflow, so both are named here rather than left to
|
||||
# SQLAlchemy's invisible default of 10.
|
||||
db_pool_size: int = 15
|
||||
db_max_overflow: int = 10
|
||||
db_echo: bool = False
|
||||
|
||||
# --- http -------------------------------------------------------------
|
||||
@@ -72,8 +79,11 @@ class Settings(BaseSettings):
|
||||
rate_limit_enabled: bool = True
|
||||
|
||||
# --- worker -----------------------------------------------------------
|
||||
worker_poll_interval_s: float = 2.0
|
||||
worker_batch_size: int = 1
|
||||
# Idle polling only: the loop drains a non-empty queue back to back without
|
||||
# sleeping, so this is the delay before a *newly* enqueued job is noticed,
|
||||
# not a per-job cost. 5s trades latency nobody can perceive on a bird camera
|
||||
# for 12 idle queries a minute instead of 30.
|
||||
worker_poll_interval_s: float = 5.0
|
||||
job_max_attempts: int = 3
|
||||
|
||||
# --- logging ----------------------------------------------------------
|
||||
|
||||
@@ -31,6 +31,11 @@ if not _settings.database_url_is_set:
|
||||
engine = create_engine(
|
||||
_settings.database_url,
|
||||
pool_size=_settings.db_pool_size,
|
||||
max_overflow=_settings.db_max_overflow,
|
||||
# Past pool_size + max_overflow a caller waits rather than failing. It has
|
||||
# to be finite: without a timeout a pool leak presents as requests hanging
|
||||
# forever instead of as an error naming the pool.
|
||||
pool_timeout=30,
|
||||
pool_pre_ping=True,
|
||||
echo=_settings.db_echo,
|
||||
future=True,
|
||||
|
||||
@@ -101,6 +101,13 @@ def run() -> int:
|
||||
)
|
||||
|
||||
try:
|
||||
# One job per claim, deliberately. The review suggested claiming in
|
||||
# batches of 5, but this loop already runs jobs back to back without
|
||||
# sleeping while the queue has work, so a batch would only save claim
|
||||
# round-trips -- and it would cost crash safety. Claiming 5 marks all 5
|
||||
# 'running'; dying after the first leaves four locked by a process that
|
||||
# no longer exists, invisible until requeue_stale gets to them up to 15
|
||||
# minutes later. Single claims keep exactly one job at risk.
|
||||
while not _stop.is_set():
|
||||
try:
|
||||
with SessionLocal() as session:
|
||||
|
||||
Reference in new issue
Block a user