-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libpq resolves the host address while PQreset, but ruby-pg doesn't. T…
…his is because we explicit set the `hostaddr` connection parameter when the connection is established the first time. This prevents a newly DNS resolution when running PQresetStart. This patch adds DNS resolution to `conn.reset` Since we can not change the connection parameters after connection start, the underlying PGconn pointer is exchanged in reset_start2. This is done by a PQfinish() + PQconnectStart() sequence. That way the `hostaddr` parameter is updated and a new connection is established with it. There is a `/etc/hosts` and `sudo` based test in the specs. The behavior of libpq is slightly different to that of ruby-pg. It can be verified by the following code: ```ruby require "pg" puts "pg version: #{PG::VERSION}" system "sudo sed -i 's/.* abcd/::1 abcd/g' /etc/hosts" conn = PG.connect host: "abcd", password: "l" conn.exec("select 1") p conn.conninfo_hash.slice(:host, :hostaddr, :port) system "sudo sed -i 's/.* abcd/127.0.0.1 abcd/g' /etc/hosts" conn.reset conn.exec("select 1") p conn.conninfo_hash.slice(:host, :hostaddr, :port) system "sudo sed -i 's/.* abcd/::2 abcd/g' /etc/hosts" conn.reset conn.exec("select 1") p conn.conninfo_hash.slice(:host, :hostaddr, :port) ``` This gives the following output showing, that the IP address is updated: ``` pg version: 1.5.5 {:host=>"abcd", :hostaddr=>"::1", :port=>"5432"} {:host=>"abcd", :hostaddr=>"127.0.0.1", :port=>"5432"} ruby-pg/lib/pg/connection.rb:573:in `reset_start2': connection to server at "::2", port 5432 failed: Network is unreachable (PG::ConnectionBad) Is the server running on that host and accepting TCP/IP connections? ``` Whereas libpq resolves similarly with `async_api=false`, but doesn't raise the error in `conn.reset` but in the subsequent `conn.exec`. ``` pg version: 1.5.5 {:host=>"abcd", :hostaddr=>nil, :port=>"5432"} {:host=>"abcd", :hostaddr=>nil, :port=>"5432"} test-reset-dns.rb:18:in `sync_exec': no connection to the server (PG::UnableToSend) ``` Fixes #558
- Loading branch information
Showing
4 changed files
with
85 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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