Skip to content

Instantly share code, notes, and snippets.

@jdunphy
Created November 5, 2009 19:26
Show Gist options
  • Save jdunphy/227314 to your computer and use it in GitHub Desktop.
Save jdunphy/227314 to your computer and use it in GitHub Desktop.
def socket
%return @sock if @sock and not @sock.closed?
@sock = nil
# If the host was dead, don't retry for a while.
return if @retry and @retry > Time.now
# Attempt to connect if not already connected.
begin
@sock = connect_to(@host, @port, @timeout)
@sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
@retry = nil
@status = 'CONNECTED'
rescue SocketError, SystemCallError, IOError => err
logger.warn { "Unable to open socket: #{err.class.name}, #{err.message}" } if logger
mark_dead err
 end
return @sock
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment