Sha256: 228d27dbd46f57ee1e5491de65ba15a9aeb107b6888b9ed770a0ca46cdb88ccf

Contents?: true

Size: 874 Bytes

Versions: 97

Compression:

Stored size: 874 Bytes

Contents

# frozen_string_literal: true

require 'polyphony'

def try_connect(ip_address, port, supervisor)
  puts "trying #{ip_address}"
  sleep rand * 0.2
  socket = TCPSocket.new(ip_address, port)
  puts "connected to #{ip_address}"
  supervisor.schedule [ip_address, socket]
rescue IOError, SystemCallError
  # ignore error
end

def happy_eyeballs(hostname, port, max_wait_time: 0.010)
  targets = Socket.getaddrinfo(hostname, port, :INET, :STREAM)
  t0 = Time.now
  fibers = []
  supervisor = Fiber.current
  spin do
    targets.each do |t|
      spin { try_connect(t[2], t[1], supervisor) }
      sleep(max_wait_time)
    end
    suspend
  end
  target, socket = move_on_after(5) { suspend }
  supervisor.shutdown_all_children
  if target
    puts format('success: %s (%.3fs)', target, Time.now - t0)
  else
    puts 'timed out'
  end
end

happy_eyeballs('debian.org', 'https')

Version data entries

97 entries across 97 versions & 1 rubygems

Version Path
polyphony-1.6 examples/io/happy-eyeballs.rb
polyphony-1.5 examples/io/happy-eyeballs.rb
polyphony-1.4 examples/io/happy-eyeballs.rb
polyphony-1.3 examples/io/happy-eyeballs.rb
polyphony-1.2.1 examples/io/happy-eyeballs.rb
polyphony-1.2 examples/io/happy-eyeballs.rb
polyphony-1.1.1 examples/io/happy-eyeballs.rb
polyphony-1.1 examples/io/happy-eyeballs.rb
polyphony-1.0.2 examples/io/happy-eyeballs.rb
polyphony-1.0.1 examples/io/happy-eyeballs.rb
polyphony-1.0 examples/io/happy-eyeballs.rb
polyphony-0.99.6 examples/io/happy-eyeballs.rb
polyphony-0.99.5 examples/io/happy-eyeballs.rb
polyphony-0.99.4 examples/io/happy-eyeballs.rb
polyphony-0.99.3 examples/io/happy-eyeballs.rb
polyphony-0.99.2 examples/io/happy-eyeballs.rb
polyphony-0.99.1 examples/io/happy-eyeballs.rb
polyphony-0.99 examples/io/happy-eyeballs.rb
polyphony-0.98 examples/io/happy-eyeballs.rb
polyphony-0.97 examples/io/happy-eyeballs.rb