Description
I am experiencing this issue on a Windows machine. Original issue filed with Windows Ruby installer: oneclick/rubyinstaller2#264
It seems that the change from TcpSocket
to Socket
(#10) released in Ruby 3.1 is now causing timeout latency for me when accessing IPv6 sites using Net::HTTP. Socket.tcp
waits 60 sec attempting connect to IPv6, times out, then immediately connects to IPv4. TCPSocket.open
does not have this issue.
TCPSocket.open("google.com", 80, nil, nil) # fast
Socket.tcp("google.com", 80, nil, nil, connect_timeout: 60) # times out after 60 sec on IPv6, then falls back to IPv4
TCPSocket.open
is fast on both Ruby 3.0 and 3.1, while Socket.tcp
is slow on both Ruby 3.0 and 3.1. However, since Net::HTTP switched to Socket in 3.1 it has the end-result of making Net::HTTP slow overall.
The issue may be Windows-specific, since I do not see when using Linux via WSL Ubuntu on the same Windows machine. There's a lot more details in the rubyinstaller2 issue, however, I'm wondering if someone can provide some insight on how to debug this further.
Script to reproduce issue:
require 'uri'
require 'net/http'
require 'openssl'
module HTTPS_IPv4_IPv6
class << self
def run(uri_str)
uri = URI(uri_str)
puts uri_str
t_st = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Net::HTTP.start(uri.host, uri.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_PEER) do |http|
req = Net::HTTP::Get.new uri.request_uri
resp = http.request req
case resp
when Net::HTTPSuccess
body = resp.body
t_end = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "body bytesize #{body.bytesize}"
puts format("response time %6.3f sec", t_end - t_st)
puts http.instance_variable_get(:@socket).io.to_io.remote_address.inspect, ''
when Net::HTTPRedirection
puts "#{resp.class}\n Redirect: #{resp['location']}"
run resp['location']
else
puts resp.class
end
end
end
end
end
puts '', RUBY_DESCRIPTION, ''
HTTPS_IPv4_IPv6.run 'https://www.google.com'
HTTPS_IPv4_IPv6.run 'https://global.jd.com'
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32]
https://www.google.com
body bytesize 15110
response time 2.390 sec
#<Addrinfo: 142.251.42.196:443 TCP>
https://global.jd.com -- IPv4 only
body bytesize 20592
response time 0.633 sec
#<Addrinfo: 14.0.43.163:443 TCP>