feat: split the connection factory into plan + failover loop#24
Merged
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Delivers candidate 2 from the architecture review.
build_connection_factorywas one 79-line closure that entangled pure URL-parse/host-ordering with async connect+failover, and wroteasyncpg.connect(...)twice. This splits it:build_connection_plan(url) -> ConnectionPlan— the pure parse phase (no I/O), returning a frozen/slottedConnectionPlan(connect_args,target_session_attrs,primary_host/primary_port,failoverpairs). Internal seam — not in__all__.build_connection_factory— a thin loop that builds the plan once and routes both the primary and per-host attempts through a single_connecthelper (the oneasyncpg.connectcall).Behaviour-preserving: same connect kwargs, same two-stage strategy, same exceptions, same log strings, same parse-once timing, and the same two shuffle lifetimes (bulk order shuffled once at build; failover re-shuffled per connection via
_reshuffled).Wins (in the design vocabulary)
asyncpg.connect— the bug-prone host/port pairing (zip(strict=True)+ shuffle + split) now has direct, mock-free coverage._connectis the sole call site.build_connection_factory's public signature is unchanged.Changes
db_retry/connections.py—ConnectionPlan+build_connection_plan+_connect+_reshuffled; factory rewritten as the thin loop.tests/test_connection_factory.py— two mock-free plan tests (order-independent pairing/branch assertions); the four loop tests kept (orchestration coverage).architecture/connections.md— promoted to the split.planning/changes/2026-06-26.03-connection-plan-split/.Verification
just test(Docker Postgres) → 27 passed, 100% coveragejust lint-ci→ clean;just check-planning→planning: OK🤖 Generated with Claude Code