Thanks to visit codestin.com
Credit goes to github.com

Skip to content

net/http: use connect_timeout in Socket.tcp #1806

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

Closed
Closed
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
12 changes: 9 additions & 3 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@ def ipaddr=(addr)
# 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,
Expand Down Expand Up @@ -981,14 +986,15 @@ def connect
end

D "opening connection to #{conn_addr}:#{conn_port}..."
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
s =
begin
TCPSocket.open(conn_addr, conn_port, @local_host, @local_port)
Socket.tcp(conn_address, conn_port, @local_host, @local_port, connect_timeout: @open_timeout, resolv_timeout: @resolv_timeout)
rescue Errno::ETIMEDOUT
raise Net::OpenTimeout
rescue => e
raise e, "Failed to open TCP connection to " +
"#{conn_addr}:#{conn_port} (#{e.message})"
end
}
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
D "opened"
if use_ssl?
Expand Down
24 changes: 24 additions & 0 deletions spec/ruby/library/net/http/http/resolv_timeout_spec.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions spec/ruby/library/socket/socket/tcp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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