Class: Sensu::Server::Socket

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/sensu/server/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#on_errorProc

Returns callback to be called when there is an error.

Returns:

  • (Proc)

    callback to be called when there is an error.



11
12
13
# File 'lib/sensu/server/socket.rb', line 11

def on_error
  @on_error
end

#on_successProc

Returns callback to be called after the data has been transmitted successfully.

Returns:

  • (Proc)

    callback to be called after the data has been transmitted successfully.



7
8
9
# File 'lib/sensu/server/socket.rb', line 7

def on_success
  @on_success
end

Instance Method Details

#connection_completedObject

Record the current time when connected.



28
29
30
# File 'lib/sensu/server/socket.rb', line 28

def connection_completed
  @connected_at = Time.now.to_f
end

#set_timeout(timeout) ⇒ Object

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.

Parameters:

  • timeout (Numeric)

    in seconds.



20
21
22
23
24
25
# 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

#unbindObject

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.



40
41
42
43
44
45
46
47
48
49
50
51
# 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