Module: Ohai::Mixin::HttpHelper

Defined in:
lib/ohai/mixin/http_helper.rb

Instance Method Summary collapse

Instance Method Details

#can_socket_connect?(addr, port, timeout = 2) ⇒ Boolean

see if we can socket connect to an address/port

Parameters:

  • addr (String)

    the address to connect to

  • port (Integer)

    the port to connect to

  • timeout (Integer) (defaults to: 2)

    the seconds before timing out

Returns:

  • (Boolean)

    can we connect?



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ohai/mixin/http_helper.rb', line 30

def can_socket_connect?(addr, port, timeout = 2)
  t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
  begin
    saddr = Socket.pack_sockaddr_in(port, addr)
  rescue SocketError => e # generally means dns resolution error
    logger.trace("Mixin HttpHelper: can_socket_connect? failed setting up socket connection: #{e}")
    return false
  end

  connected = false

  begin
    t.connect_nonblock(saddr)
  rescue Errno::EINPROGRESS
    _r, w, _e = IO.select(nil, [t], nil, timeout)
    if !w.nil?
      connected = true
    else
      begin
        t.connect_nonblock(saddr)
      rescue Errno::EISCONN
        t.close
        connected = true
      rescue SystemCallError
      end
    end
  rescue SystemCallError
  end
  logger.trace("Mixin HttpHelper: can_socket_connect? == #{connected}")
  connected
end