Split a comma-joined host list into a sequence for asyncpg - #2267
Open
afonsojanu wants to merge 1 commit into
Open
Split a comma-joined host list into a sequence for asyncpg#2267afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
A postgres DB URL with more than one host, e.g. host1,host2:5432, is libpq's own multi-host connection string syntax for primary/replica failover. urlparse reads the whole comma-joined string back as a single hostname, so asyncpg.connect() ended up being asked to resolve "host1,host2" as one DNS name and failed with a plain gaierror instead of trying each host in turn. psycopg forwards its kwargs straight through to libpq, which already understands the joined string on its own, so only the asyncpg path needed the split into an actual list of hosts, which is the form asyncpg's own host parameter accepts for this exact case.
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.
Description
A postgres DB URL with more than one host, e.g.
postgres://user:pass@host1,host2:5432/db, is libpq's own multi-host syntax for primary/replica failover.urlparsereads the whole comma-joined string back as a single hostname, soasyncpg.connect()ended up being asked to resolvehost1,host2as one DNS name and failed with a plainsocket.gaierrorinstead of trying each host in turn.expand_db_urlnow splits the hostname into a list when it contains a comma and the backend is one ofpostgres/postgresql/asyncpg, since that's the form asyncpg's ownhostparameter accepts for exactly this case (it documents accepting "a sequence" of hosts, tried in order). psycopg is left untouched: it forwards its kwargs straight to libpq, which already parses the joined string correctly on its own, so splitting it there would just make libpq see a Python list where it expects the original string.Motivation and Context
Fixes #1924
How Has This Been Tested?
Added
test_postgres_multi_hosttotests/backends/test_db_url.py, covering all four postgres-family schemes (postgres,postgresql,asyncpg,psycopg) against the same multi-host URL, asserting the asyncpg-family schemes get a list and psycopg keeps the joined string. Confirmed viagit stashthat it fails against the unmodified code (asserts a list where the code still returns the joined string) and passes with the fix.python -m pytest tests/backends/test_db_url.py tests/test_connection.py: 59 passed.ruff checkandruff format --check: clean.mypyon the changed file: 0 errors.Checklist: