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
  • Loading branch information
mohamedhafez committed Feb 15, 2021
1 parent e4bbf95 commit 6ae4a59
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/net/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,12 @@ def finish
private

def tcp_socket(address, port)
TCPSocket.open address, port
begin
Socket.tcp address, 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 "\
"#{address}:#{port} (exceeds #{@open_timeout} seconds)"
end
end

def do_start(helo_domain, user, secret, authtype)
Expand All @@ -584,9 +589,7 @@ def do_start(helo_domain, user, secret, authtype)
check_auth_method(authtype || DEFAULT_AUTH_TYPE)
check_auth_args user, secret
end
s = Timeout.timeout(@open_timeout, Net::OpenTimeout) do
tcp_socket(@address, @port)
end
s = tcp_socket(@address, @port)
logging "Connection opened: #{@address}:#{@port}"
@socket = new_internet_message_io(tls? ? tlsconnect(s, @ssl_context_tls) : s)
check_response critical { recv_response() }
Expand Down

0 comments on commit 6ae4a59

Please sign in to comment.