Skip to content

Commit d659101

Browse files
committed
Replace Timeout.timeout with socket timeout
Timeout.timeout is inefficient since it spins up a new thread for each invocation, use Socket.tcp's connect_timeout option instead when we aren't using SOCKS (we can't replace Timeout.timeout for SOCKS yet since SOCKSSocket doesn't have a connect_timeout option).
1 parent 827e471 commit d659101

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/net/ftp.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,19 @@ def return_code=(s) # :nodoc:
330330
# SOCKS_SERVER, then a SOCKSSocket is returned, else a Socket is
331331
# returned.
332332
def open_socket(host, port) # :nodoc:
333-
return Timeout.timeout(@open_timeout, OpenTimeout) {
334-
if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
335-
@passive = true
333+
if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
334+
@passive = true
335+
Timeout.timeout(@open_timeout, OpenTimeout) do
336336
SOCKSSocket.open(host, port)
337-
else
338-
Socket.tcp(host, port)
339337
end
340-
}
338+
else
339+
begin
340+
Socket.tcp host, port, nil, nil, connect_timeout: @open_timeout
341+
rescue Errno::ETIMEDOUT #raise Net:OpenTimeout instead for compatibility with previous versions
342+
raise Net::OpenTimeout, "Timeout to open TCP connection to "\
343+
"#{host}:#{port} (exceeds #{@open_timeout} seconds)"
344+
end
345+
end
341346
end
342347
private :open_socket
343348

0 commit comments

Comments
 (0)