diff --git a/lib/net/http.rb b/lib/net/http.rb index 663b901a96..4723cfe9d1 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -733,6 +733,11 @@ def set_debug_output(output) # Net::OpenTimeout exception. The default value is 60 seconds. attr_accessor :open_timeout + # Specify the DNS name resolution timeout in seconds. If the name + # cannot be resolved in this many seconds, it raises a + # SocketError exception. The default value is set by operation system. + attr_accessor :resolv_timeout + # Number of seconds to wait for one block to be read (via one read(2) # call). Any number may be used, including Floats for fractional # seconds. If the HTTP object cannot read data in this many seconds, @@ -942,14 +947,12 @@ def connect end D "opening connection to #{conn_address}:#{conn_port}..." - s = Timeout.timeout(@open_timeout, Net::OpenTimeout) { - begin - TCPSocket.open(conn_address, conn_port, @local_host, @local_port) - rescue => e - raise e, "Failed to open TCP connection to " + - "#{conn_address}:#{conn_port} (#{e.message})" - end - } + s = begin + Socket.tcp(conn_address, conn_port, @local_host, @local_port, connect_timeout: @open_timeout, resolv_timeout: @resolv_timeout) + rescue => e + raise e, "Failed to open TCP connection to " + + "#{conn_address}:#{conn_port} (#{e.message})" + end s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) D "opened" if use_ssl? diff --git a/spec/ruby/library/net/http/http/resolv_timeout_spec.rb b/spec/ruby/library/net/http/http/resolv_timeout_spec.rb new file mode 100644 index 0000000000..7817bb00c1 --- /dev/null +++ b/spec/ruby/library/net/http/http/resolv_timeout_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../../../spec_helper' +require 'net/http' + +describe "Net::HTTP#resolv_timeout" do + it "returns the seconds to wait until reading one block" do + net = Net::HTTP.new("localhost") + net.resolv_timeout.should eql(nil) + net.resolv_timeout = 10 + net.resolv_timeout.should eql(10) + end +end + +describe "Net::HTTP#resolv_timeout=" do + it "sets the seconds to wait till the connection is open" do + net = Net::HTTP.new("localhost") + net.resolv_timeout = 10 + net.resolv_timeout.should eql(10) + end + + it "returns the newly set value" do + net = Net::HTTP.new("localhost") + (net.resolv_timeout = 10).should eql(10) + end +end diff --git a/spec/ruby/library/socket/socket/tcp_spec.rb b/spec/ruby/library/socket/socket/tcp_spec.rb index d36e3e6adb..95d50e9a65 100644 --- a/spec/ruby/library/socket/socket/tcp_spec.rb +++ b/spec/ruby/library/socket/socket/tcp_spec.rb @@ -67,4 +67,10 @@ connection.close end end + + it 'accepts tcp timeout' do + @client = Socket.tcp(@host, @port, resolv_timeout: 1) + @client.write('hello') + @client.close + end end