Skip to content

Ajmaidak try next host on timeout #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/binary-gems.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
fail-fast: false
matrix:
include:
- platform: "x64-mingw-ucrt"
- platform: "x64-mingw32"
- platform: "x86-mingw32"
- platform: "x86_64-linux"
# - platform: "x64-mingw-ucrt"
# - platform: "x64-mingw32"
# - platform: "x86-mingw32"
# - platform: "x86_64-linux"
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
Expand Down
56 changes: 28 additions & 28 deletions .github/workflows/source-gem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,34 @@ jobs:
fail-fast: false
matrix:
include:
- os: windows
ruby: "head"
PGVERSION: 17.0-1-windows-x64
PGVER: "17"
- os: windows
ruby: "2.7"
PGVERSION: 10.20-1-windows-x64
PGVER: "10"
- os: windows
ruby: "mswin"
PGVERSION: 17.0-1-windows-x64
PGVER: "17"
- os: ubuntu
ruby: "head"
PGVER: "17"
- os: ubuntu
ruby: "3.2"
PGVER: "12"
- os: ubuntu
os_ver: "20.04"
ruby: "2.7"
PGVER: "10"
- os: ubuntu
ruby: "truffleruby"
PGVER: "14"
- os: ubuntu
ruby: "truffleruby-head"
PGVER: "17"
# - os: windows
# ruby: "head"
# PGVERSION: 17.0-1-windows-x64
# PGVER: "17"
# - os: windows
# ruby: "2.7"
# PGVERSION: 10.20-1-windows-x64
# PGVER: "10"
# - os: windows
# ruby: "mswin"
# PGVERSION: 17.0-1-windows-x64
# PGVER: "17"
# - os: ubuntu
# ruby: "head"
# PGVER: "17"
# - os: ubuntu
# ruby: "3.2"
# PGVER: "12"
# - os: ubuntu
# os_ver: "20.04"
# ruby: "2.7"
# PGVER: "10"
# - os: ubuntu
# ruby: "truffleruby"
# PGVER: "14"
# - os: ubuntu
# ruby: "truffleruby-head"
# PGVER: "17"
- os: macos
ruby: "head"
PGVERSION: 17.0-1-osx
Expand Down
16 changes: 15 additions & 1 deletion lib/pg/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ module Pollable
host_count = conninfo_hash[:host].to_s.count(",") + 1
stop_time = timeo * host_count + Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
connection_errors = []

poll_status = PG::PGRES_POLLING_WRITING
until poll_status == PG::PGRES_POLLING_OK ||
Expand Down Expand Up @@ -720,7 +721,13 @@ module Pollable
else
connhost = "at \"#{host}\", port #{port}"
end
raise PG::ConnectionBad.new("connection to server #{connhost} failed: timeout expired", connection: self)
connection_errors << "connection to server #{connhost} failed: timeout expired"
if connection_errors.count < host_count.to_i
new_conninfo_hash = rotate_hosts(conninfo_hash.compact)
send(:reset_start2, self.class.send(:parse_connect_args, new_conninfo_hash))
else
raise PG::ConnectionBad.new(connection_errors.join("\n"), connection: self)
end
end

# Check to see if it's finished or failed yet
Expand All @@ -733,6 +740,13 @@ module Pollable
raise PG::ConnectionBad.new(msg, connection: self)
end
end

private def rotate_hosts(conninfo_hash)
conninfo_hash[:host] = conninfo_hash[:host].split(",").rotate.join(",") if conninfo_hash[:host]
conninfo_hash[:port] = conninfo_hash[:port].split(",").rotate.join(",") if conninfo_hash[:port]
conninfo_hash[:hostaddr] = conninfo_hash[:hostaddr].split(",").rotate.join(",") if conninfo_hash[:hostaddr]
conninfo_hash
end
end

include Pollable
Expand Down
11 changes: 11 additions & 0 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ def create_cert_from_csr(name, csr, ca_cert, ca_key, valid_years: 10, dns_names:
end
end

class ListenSocket
attr_reader :port
def initialize(host = 'localhost', accept: true)
TCPServer.open( host, 0 ) do |serv|

@port = serv.local_address.ip_port
yield self
end
end
end

def check_for_lingering_connections( conn )
conn.exec( "SELECT * FROM pg_stat_activity" ) do |res|
conns = res.find_all {|row| row['pid'].to_i != conn.backend_pid && ["client backend", nil].include?(row["backend_type"]) }
Expand Down
61 changes: 40 additions & 21 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,24 +369,40 @@
end
end

it "times out after connect_timeout seconds" do
TCPServer.open( 'localhost', 54320 ) do |serv|
it "times out after 2 * connect_timeout seconds on two connections" do
PG::TestingHelpers::ListenSocket.new do |sock|
start_time = Time.now
expect {
described_class.connect(
host: 'localhost',
port: 54320,
connect_timeout: 1,
dbname: "test")
host: 'localhost,localhost',
port: sock.port,
connect_timeout: 1,
dbname: "test")
}.to raise_error do |error|
expect( error ).to be_an( PG::ConnectionBad )
expect( error.message ).to match( /timeout expired/ )
if PG.library_version >= 120000
expect( error.message ).to match( /\"localhost\"/ )
expect( error.message ).to match( /port 54320/ )
if PG.library_version >= 140000
expect( error.message ).to match( /timeout expired.*timeout expired/m )
expect( error.message ).to match( /\"localhost\".*\"localhost\"/m )
expect( error.message ).to match( /port #{sock.port}/ )
else
expect( error.message ).to match( /timeout expired/m )
end
end

expect( Time.now - start_time ).to be_between(1.9, 10).inclusive
end
end

it "succeeds with second host after connect_timeout" do
PG::TestingHelpers::ListenSocket.new do |sock|
start_time = Time.now
conn = described_class.connect(
host: 'localhost,localhost,localhost',
port: "#{sock.port},#{@port},#{sock.port}",
connect_timeout: 1,
dbname: "test")

expect( conn.port ).to eq( @port )
expect( Time.now - start_time ).to be_between(0.9, 10).inclusive
end
end
Expand Down Expand Up @@ -768,7 +784,8 @@
end

it "raises proper error when sending fails" do
conn = described_class.connect_start( '127.0.0.1', 54320, "", "", "me", "xxxx", "somedb" )
sock = PG::TestingHelpers::ListenSocket.new('127.0.0.1', accept: false){ }
conn = described_class.connect_start( '127.0.0.1', sock.port, "", "", "me", "xxxx", "somedb" )
expect{ conn.exec 'SELECT 1' }.to raise_error(PG::UnableToSend, /no connection/){|err| expect(err).to have_attributes(connection: conn) }
end

Expand Down Expand Up @@ -1674,11 +1691,12 @@


it "handles server close while asynchronous connect" do
serv = TCPServer.new( '127.0.0.1', 54320 )
conn = described_class.connect_start( '127.0.0.1', 54320, "", "", "me", "xxxx", "somedb" )
expect( [PG::PGRES_POLLING_WRITING, PG::CONNECTION_OK] ).to include conn.connect_poll
select( nil, [conn.socket_io], nil, 0.2 )
serv.close
conn = nil
PG::TestingHelpers::ListenSocket.new('127.0.0.1', accept: false) do |sock|
conn = described_class.connect_start( '127.0.0.1', sock.port, "", "", "me", "xxxx", "somedb" )
expect( [PG::PGRES_POLLING_WRITING, PG::CONNECTION_OK] ).to include conn.connect_poll
select( nil, [conn.socket_io], nil, 0.2 )
end
if conn.connect_poll == PG::PGRES_POLLING_READING
select( [conn.socket_io], nil, nil, 0.2 )
end
Expand Down Expand Up @@ -1802,12 +1820,13 @@
end

it "consume_input should raise ConnectionBad for a closed connection" do
serv = TCPServer.new( '127.0.0.1', 54320 )
conn = described_class.connect_start( '127.0.0.1', 54320, "", "", "me", "xxxx", "somedb" )
while [PG::CONNECTION_STARTED, PG::CONNECTION_MADE].include?(conn.connect_poll)
sleep 0.1
conn = nil
PG::TestingHelpers::ListenSocket.new '127.0.0.1', accept: false do |sock|
conn = described_class.connect_start( '127.0.0.1', sock.port, "", "", "me", "xxxx", "somedb" )
while [PG::CONNECTION_STARTED, PG::CONNECTION_MADE].include?(conn.connect_poll)
sleep 0.1
end
end
serv.close
expect{ conn.consume_input }.to raise_error(PG::ConnectionBad, /server closed the connection unexpectedly/){|err| expect(err).to have_attributes(connection: conn) }
expect{ conn.consume_input }.to raise_error(PG::ConnectionBad, /can't get socket descriptor|connection not open/){|err| expect(err).to have_attributes(connection: conn) }
end
Expand Down
Loading