summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/net/http.rb22
1 files changed, 15 insertions, 7 deletions
diff --git a/lib/net/http.rb b/lib/net/http.rb
index 7efb468e78..d6db9ba132 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -1672,14 +1672,22 @@ module Net #:nodoc:
end
debug "opening connection to #{conn_addr}:#{conn_port}..."
- s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
- begin
- TCPSocket.open(conn_addr, conn_port, @local_host, @local_port)
- rescue => e
- raise e, "Failed to open TCP connection to " +
- "#{conn_addr}:#{conn_port} (#{e.message})"
+ begin
+ s = begin
+ # Use built-in timeout in TCPSocket.open if available
+ TCPSocket.open(conn_addr, conn_port, @local_host, @local_port, open_timeout: @open_timeout)
+ rescue ArgumentError => e
+ raise if !e.message.include?('unknown keyword: :open_timeout')
+ # Fallback to Timeout.timeout if TCPSocket.open does not support open_timeout
+ Timeout.timeout(@open_timeout, Net::OpenTimeout) {
+ TCPSocket.open(conn_addr, conn_port, @local_host, @local_port)
+ }
end
- }
+ rescue => e
+ e = Net::OpenTimeout.new(e) if e.is_a?(Errno::ETIMEDOUT) # for compatibility with previous versions
+ raise e, "Failed to open TCP connection to " +
+ "#{conn_addr}:#{conn_port} (#{e.message})"
+ end
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
debug "opened"
if use_ssl?