Sha256: e5b5b07f839775947d0e23206dcef5d8e89768c33c106da50b45891d6fe48e6e

Contents?: true

Size: 969 Bytes

Versions: 18

Compression:

Stored size: 969 Bytes

Contents

# frozen_string_literal: true

# idea taken from the example given in trio:
# https://www.youtube.com/watch?v=oLkfnc_UMcE

require 'bundler/setup'
require 'polyphony'

def try_connect(target, supervisor)
  puts "trying #{target[2]}"
  socket = Polyphony::Net.tcp_connect(target[2], 80)
  # connection successful
  supervisor.stop([target[2], socket])
rescue IOError, SystemCallError
  # ignore error
end

def happy_eyeballs(hostname, port, max_wait_time: 0.025)
  targets = Socket.getaddrinfo(hostname, port, :INET, :STREAM)
  t0 = Time.now
  cancel_after(5) do
    success = supervise do |supervisor|
      targets.each_with_index do |t, idx|
        sleep(max_wait_time) if idx > 0
        supervisor.spin { try_connect(t, supervisor) }
      end
    end
    if success
      puts format('success: %s (%.3fs)', success[0], Time.now - t0)
    else
      puts "timed out (#{Time.now - t0}s)"
    end
  end
end

# Let's try it out:
happy_eyeballs('debian.org', 'https')

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
polyphony-0.43.5 examples/io/xx-happy-eyeballs.rb
polyphony-0.43.4 examples/io/xx-happy-eyeballs.rb
polyphony-0.43.3 examples/io/xx-happy-eyeballs.rb
polyphony-0.43.2 examples/io/xx-happy-eyeballs.rb
polyphony-0.43.1 examples/io/xx-happy-eyeballs.rb
polyphony-0.43 examples/io/xx-happy-eyeballs.rb
polyphony-0.42 examples/io/xx-happy-eyeballs.rb
polyphony-0.41 examples/io/xx-happy-eyeballs.rb
polyphony-0.40 examples/io/xx-happy-eyeballs.rb
polyphony-0.39 examples/io/xx-happy-eyeballs.rb
polyphony-0.38 examples/io/xx-happy-eyeballs.rb
polyphony-0.36 examples/io/xx-happy-eyeballs.rb
polyphony-0.34 examples/io/xx-happy-eyeballs.rb
polyphony-0.33 examples/io/xx-happy-eyeballs.rb
polyphony-0.32 examples/io/xx-happy-eyeballs.rb
polyphony-0.31 examples/io/xx-happy-eyeballs.rb
polyphony-0.30 examples/io/xx-happy-eyeballs.rb
polyphony-0.29 examples/io/xx-happy-eyeballs.rb