module Ohai::Mixin::HttpHelper

Public Instance Methods

can_socket_connect?(addr, port, timeout = 2) click to toggle source
# File lib/ohai/mixin/http_helper.rb, line 23
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
    Ohai::Log.debug("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
  Ohai::Log.debug("Mixin HttpHelper: can_socket_connect? == #{connected}")
  connected
end