class Sensu::Server::Socket

Attributes

on_error[RW]

@!attribute [rw] #on_error @return [Proc] callback to be called when there is an error.

on_success[RW]

@!attribute [rw] #on_success @return [Proc] callback to be called after the data has been

transmitted successfully.

Public Instance Methods

connection_completed() click to toggle source

Record the current time when connected.

# File lib/sensu/server/socket.rb, line 28
def connection_completed
  @connected_at = Time.now.to_f
end
set_timeout(timeout) click to toggle source

Create a timeout timer to immediately close the socket connection and set `@timed_out` to true to indicate that the timeout caused the connection to close. The timeout timer is stored with `@timeout_timer`, so that it can be cancelled when the connection is closed.

@param timeout [Numeric] in seconds.

# File lib/sensu/server/socket.rb, line 20
def set_timeout(timeout)
  @timeout_timer = EM::Timer.new(timeout) do
    @timed_out = true
    close_connection
  end
end
unbind() click to toggle source

Determine if the connection and data transmission was successful and call the appropriate callback, `@on_success` or `@on_error`, providing it with a message. Cancel the connection timeout timer `@timeout_timer`, if it is set. The `@connected_at` timestamp indicates that the connection was successful. If `@timed_out` is true, the connection was closed by the connection timeout, and the data is assumed to not have been transmitted.

# File lib/sensu/server/socket.rb, line 40
def unbind
  @timeout_timer.cancel if @timeout_timer
  if @connected_at
    if @timed_out
      @on_error.call("socket timeout")
    else
      @on_success.call("wrote to socket")
    end
  else
    @on_error.call("failed to connect to socket")
  end
end