From 6ae4a59f05b603fec0737f265ef02e0ea1ffc4fb Mon Sep 17 00:00:00 2001 From: mohamed Date: Mon, 15 Feb 2021 09:36:50 -0800 Subject: [PATCH] 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 --- lib/net/smtp.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index a97c0c3..b1ba175 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -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) @@ -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() }