Module: Hula::Helpers::SocketTools

Included in:
Hula::HttpProxyUpstreamSocks, Socks4ProxySsh
Defined in:
lib/hula/helpers/socket_tools.rb

Class Method Summary collapse

Class Method Details

.free_portObject



33
34
35
36
37
38
39
# File 'lib/hula/helpers/socket_tools.rb', line 33

module_function def free_port
  socket = Socket.new(:INET, :STREAM, 0)
  socket.bind(Addrinfo.tcp('127.0.0.1', 0))
  socket.local_address.ip_port
ensure
  socket.close
end

.port_open?(host:, port:) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File 'lib/hula/helpers/socket_tools.rb', line 25

module_function def port_open?(host:, port:)
  socket = TCPSocket.new(host, port)
  socket.close unless socket.nil?
  true
rescue Errno::ECONNREFUSED
  false
end

.wait_for_port(host:, port:, timeout_seconds: 20) ⇒ Object



18
19
20
21
22
23
# File 'lib/hula/helpers/socket_tools.rb', line 18

module_function def wait_for_port(host:, port:, timeout_seconds: 20)
  error = "Failed to connect to #{host}:#{port} within #{timeout_seconds} seconds"
  TimeoutTools.wait_for(error: error, timeout_seconds: timeout_seconds) do
    port_open?(host: host, port: port)
  end
end