Sha256: ed9a58e5e5fdb9b9319de171097663218ab9e6a8aa9ee386a41fbdcd720b481d

Contents?: true

Size: 975 Bytes

Versions: 9

Compression:

Stored size: 975 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/http'

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

9 entries across 9 versions & 2 rubygems

Version Path
polyphony-http-0.28 examples/happy_eyeballs.rb
polyphony-http-0.27 examples/happy_eyeballs.rb
polyphony-http-0.26 examples/happy_eyeballs.rb
polyphony-http-0.25 examples/happy_eyeballs.rb
polyphony-http-0.24 examples/happy_eyeballs.rb
polyphony-0.23 examples/http/happy_eyeballs.rb
polyphony-0.22 examples/http/happy_eyeballs.rb
polyphony-0.21 examples/http/happy_eyeballs.rb
polyphony-0.20 examples/http/happy_eyeballs.rb