Sha256: ab6ef2b389a24cbc9d39f3aecf4f88a100f396471d50ebf7351ca0fae8ed69b3
Contents?: true
Size: 1.3 KB
Versions: 2
Compression:
Stored size: 1.3 KB
Contents
require 'socket' module CoreExtensions module TCPSocketExt def self.prepended(base) base.prepend Initializer end module Initializer CONNECTION_TIMEOUT = 5 IPV4_DELAY_SECONDS = 0.1 def initialize(host, serv, *rest) mutex = Mutex.new addrs = [] cond_var = ConditionVariable.new Addrinfo.foreach(host, serv, nil, :STREAM) do |addr| Thread.report_on_exception = false if defined? Thread.report_on_exception = () Thread.new(addr) do # give head start to ipv6 addresses sleep IPV4_DELAY_SECONDS if addr.ipv4? # raises Errno::ECONNREFUSED when ip:port is unreachable Socket.tcp(addr.ip_address, serv, connect_timeout: CONNECTION_TIMEOUT).close mutex.synchronize do addrs << addr.ip_address cond_var.signal end end end mutex.synchronize do timeout_time = CONNECTION_TIMEOUT + Time.now.to_f while addrs.empty? && (remaining_time = timeout_time - Time.now.to_f) > 0 cond_var.wait(mutex, remaining_time) end host = addrs.shift unless addrs.empty? end super(host, serv, *rest) end end end end TCPSocket.prepend CoreExtensions::TCPSocketExt
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rubygems-update-3.2.12 | lib/rubygems/core_ext/tcpsocket_init.rb |
rubygems-update-3.2.11 | lib/rubygems/core_ext/tcpsocket_init.rb |