Skip to content

Commit

Permalink
Replace Timeout.timeout with socket timeout
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mohamedhafez committed Feb 15, 2021
1 parent 827e471 commit d659101
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/net/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,19 @@ def return_code=(s) # :nodoc:
# SOCKS_SERVER, then a SOCKSSocket is returned, else a Socket is
# returned.
def open_socket(host, port) # :nodoc:
return Timeout.timeout(@open_timeout, OpenTimeout) {
if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
@passive = true
if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
@passive = true
Timeout.timeout(@open_timeout, OpenTimeout) do
SOCKSSocket.open(host, port)
else
Socket.tcp(host, port)
end
}
else
begin
Socket.tcp host, port, nil, nil, connect_timeout: @open_timeout
rescue Errno::ETIMEDOUT #raise Net:OpenTimeout instead for compatibility with previous versions
raise Net::OpenTimeout, "Timeout to open TCP connection to "\
"#{host}:#{port} (exceeds #{@open_timeout} seconds)"
end
end
end
private :open_socket

Expand Down

0 comments on commit d659101

Please sign in to comment.