Ajmaidak try next host on timeout#637
Merged
larskanis merged 10 commits intoged:masterfrom Jul 11, 2025
Merged
Conversation
f85f5c0 to
bcd57a7
Compare
dbf2f6a to
fd306c0
Compare
libpq does not support connect_timeout when using the async api. So when using the async connection API with a multi-host connection string libpq will not timeout connections to the first host in the list and thus will not attempt to connect to any subsequent hosts in the connection string list. This fixes this by closing and reopenning the connection with a reordered connection string when connection times out. See discussion on the pgsql-hackers list discussing this "feature" of the api: https://www.postgresql.org/message-id/flat/CA%2Bmi_8YyGKA9dWELu63e%3DKL2oN-%2BFe4uca4EtFfb6uQD4Up8pw%40mail.gmail.com
in case of several hosts and async API. This is what libpq does in sync API as well.
When rotating hosts, they are tried twice, if combined with hosts which fail with some other error. When other errors occure, then add timeout errors to the libpq message list. Also finish the connection object before raise to avoid keeping fds open. Use a higher timeout on Windows, since the "Connection refused" error is raised after 2 seconds. Remove unused parameter accept: from ListenSocket
Until now the connect_timeout handling worked like so: - All hosts are passed to connect_start - If a timeout happens then the related host is removed from the hosts list - This runs until no timeout happens and either a connection is established or aborted with CONNECTION_BAD - The downside is that this might connect multiple times to one and the same host. This changes the connect_timeout handling like so: - All hosts are passed to connect_start - As soon as the host is tried to connect the related host is removed from the hosts list - This runs until no hosts are left after timeout or either a connection is established or aborted with CONNECTION_BAD - The downside is that this connects only once to hosts which are listed twice. This also fixes the "socket not connected" issue on Windows. And it harmonizes the handling of connection parameters in CancelConnection.
fd306c0 to
ee11bfa
Compare
Collaborator
Author
|
I added some documentation to this PR in commit 325e22b . |
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 #618
Closes #619