Repository navigation
Split a comma-joined host list into a sequence for asyncpg - #2267
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.
|
Thanks for this fix — the root cause analysis is clear and the implementation is minimal and correct. The bug was that Test coverage looks solid: A few non-blocking notes: 1. Per-host ports. The current implementation handles the case where the port appears only on the last host ( 2. 3. Docs. The checklist says documentation was updated, but I only see code and test changes in the diff. If the doc change is in another commit, fine — just confirming it's included. CI is green and CodSpeed shows no impact. Nice work. |
|
Closing this for now as part of a clean slate — stepping back from everything I had open across these repos. |
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: