Repository navigation
feat: shuffle the bulk connect order per connection, not per factory - #65
Merged
Merged
Conversation
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.
Closes #35.
Before this change,
build_connection_planshuffled the hosts once, when the factory was built. Every connection the factory opened then sent that same order to asyncpg for its first attempt. asyncpg tries hosts in list order and keeps the first one that matchestarget_session_attrs. Withprefer-standbyand two or more standbys, the first standby in a process's order got every connection that process opened.Now each connect picks a fresh random order for the first attempt across all hosts, and the failover pass keeps its own fresh shuffle on each call, as before.
Shape change
ConnectionPlanno longer holds anything random, so building a plan from the same URL always gives an equal value:primary_host/primary_portare removed.failoverholds the(host, port)pairs in DSN order, andhost/portare taken out ofconnect_args.connect_argskeeps thehost/portthe dialect produced. The call to asyncpg gets exactly the same arguments as before.ConnectionPlanisn't exported, so this doesn't change the public API.The Primary host entry in
CONTEXT.mdnow says the order is shuffled per connection, not stored on the plan.One side effect
A DSN with no host at all (
postgresql+asyncpg://u:p@/db) used to fail insidebuild_connection_factorywithKeyError: 'host'. Now it is passed through, and asyncpg uses its usual defaults (PGHOST, then the local socket). This matches whatis_dsn_multihostalready did with that DSN.Testing
main:build_connection_planis deterministic, and keepsfailoverin DSN order.random.Random(0).asyncpg.connect, for both the bulk order and every failover attempt.ruff,tyandeof-fixerare clean.