Class: Sensu::Extension::ConnectionHandler

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/sensu/extensions/graphite.rb

Overview

Connection Handler

Constant Summary collapse

MAX_RECONNECT_ATTEMPTS =

XXX: These should be runtime configurable.

0
MAX_RECONNECT_TIME =

Attempt no reconnects. Drop if it can’t in favor for speed.

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectedObject

Returns the value of attribute connected.



43
44
45
# File 'lib/sensu/extensions/graphite.rb', line 43

def connected
  @connected
end

#connection_poolObject

seconds



42
43
44
# File 'lib/sensu/extensions/graphite.rb', line 42

def connection_pool
  @connection_pool
end

#hostObject

Returns the value of attribute host.



43
44
45
# File 'lib/sensu/extensions/graphite.rb', line 43

def host
  @host
end

#message_queueObject

seconds



42
43
44
# File 'lib/sensu/extensions/graphite.rb', line 42

def message_queue
  @message_queue
end

#nameObject

Returns the value of attribute name.



43
44
45
# File 'lib/sensu/extensions/graphite.rb', line 43

def name
  @name
end

#portObject

Returns the value of attribute port.



43
44
45
# File 'lib/sensu/extensions/graphite.rb', line 43

def port
  @port
end

#reconnect_timerObject

Returns the value of attribute reconnect_timer.



44
45
46
# File 'lib/sensu/extensions/graphite.rb', line 44

def reconnect_timer
  @reconnect_timer
end

Instance Method Details

#close_connection(*args) ⇒ Object



60
61
62
63
64
# File 'lib/sensu/extensions/graphite.rb', line 60

def close_connection(*args)
  @is_closed = true
  @connected = false
  super(*args)
end

#comm_inactivity_timeoutObject



66
67
68
69
# File 'lib/sensu/extensions/graphite.rb', line 66

def comm_inactivity_timeout
  logger.info("Graphite: Connection to #{@name} timed out.")
  schedule_reconnect
end

#connection_completedObject



56
57
58
# File 'lib/sensu/extensions/graphite.rb', line 56

def connection_completed
  @connected = true
end

#get_reconnect_timeObject



97
98
99
100
101
102
# File 'lib/sensu/extensions/graphite.rb', line 97

def get_reconnect_time
  @reconnect_timer.get_reconnect_time(
    @max_reconnect_time,
    @connection_attempt_count
  )
end

#loggerObject



114
115
116
# File 'lib/sensu/extensions/graphite.rb', line 114

def logger
  Sensu::Logger.get
end

#post_initObject

ignore :reek:TooManyStatements



47
48
49
50
51
52
53
54
# File 'lib/sensu/extensions/graphite.rb', line 47

def post_init
  @is_closed = false
  @connection_attempt_count = 0
  @max_reconnect_time = MAX_RECONNECT_TIME
  @comm_inactivity_timeout = 0 # disable inactivity timeout
  @pending_connect_timeout = 30 # seconds
  @reconnect_timer = ExponentialDecayTimer.new
end

#receive_data(data) ⇒ Object

Override EM::Connection.receive_data to prevent it from calling puts and randomly logging non-sense to sensu-server.log



85
86
# File 'lib/sensu/extensions/graphite.rb', line 85

def receive_data(data)
end

#reconnect(time) ⇒ Object

Reconnect normally attempts to connect at the end of the tick Delay the reconnect for some seconds.



90
91
92
93
94
95
# File 'lib/sensu/extensions/graphite.rb', line 90

def reconnect(time)
  EM.add_timer(time) do
    logger.info("Graphite: Attempting to reconnect relay channel: #{@name}.")
    super(@host, @port)
  end
end

#schedule_reconnectObject



104
105
106
107
108
109
110
111
112
# File 'lib/sensu/extensions/graphite.rb', line 104

def schedule_reconnect
  unless @connected
    @connection_attempt_count += 1
    reconnect_time = get_reconnect_time
    logger.info("Graphite: Scheduling reconnect in #{@reconnect_time} seconds for relay channel: #{@name}.")
    reconnect(reconnect_time)
  end
  reconnect_time
end

#send_data(*args) ⇒ Object



79
80
81
# File 'lib/sensu/extensions/graphite.rb', line 79

def send_data(*args)
  super(*args)
end

#unbindObject



71
72
73
74
75
76
77
# File 'lib/sensu/extensions/graphite.rb', line 71

def unbind
  @connected = false
  unless @is_closed
    logger.info('Graphite: Connection closed unintentionally.')
    schedule_reconnect
  end
end