Class: Bosh::Monitor::TcpConnection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/bosh/monitor/protocols/tcp_connection.rb

Direct Known Subclasses

GraphiteConnection, TsdbConnection

Constant Summary collapse

BACKOFF_CEILING =
9
MAX_RETRIES =
35

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger_name, host, port) ⇒ TcpConnection

Returns a new instance of TcpConnection.



9
10
11
12
13
14
15
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 9

def initialize(logger_name, host, port)
  @logger_name = logger_name
  @host = host
  @port = port
  @logger = Bhm.logger
  reset_retries
end

Instance Attribute Details

#logger_nameObject (readonly)

Returns the value of attribute logger_name.



7
8
9
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 7

def logger_name
  @logger_name
end

#retriesObject (readonly)

Returns the value of attribute retries.



7
8
9
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 7

def retries
  @retries
end

Instance Method Details

#connection_completedObject



25
26
27
28
29
30
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 25

def connection_completed
  reset_retries
  @reconnecting = false
  @connected = true
  @logger.info("#{@logger_name}-connected")
end

#increment_retriesObject



21
22
23
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 21

def increment_retries
  @retries += 1
end

#receive_data(data) ⇒ Object



57
58
59
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 57

def receive_data(data)
  @logger.info("#{logger_name} << #{data.chomp}")
end

#reset_retriesObject



17
18
19
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 17

def reset_retries
  @retries = 0
end

#retry_reconnectObject



52
53
54
55
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 52

def retry_reconnect
  @logger.info("#{@logger_name}-reconnecting (#{retries})...")
  reconnect(@host, @port)
end

#unbindObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bosh/monitor/protocols/tcp_connection.rb', line 32

def unbind
  if @connected
    @logger.warn("#{@logger_name}-connection-lost")
  end
  @connected = false

  retry_in = 2**[retries, BACKOFF_CEILING].min - 1
  increment_retries

  if retries > MAX_RETRIES
    raise "#{logger_name}-failed-to-reconnect after #{MAX_RETRIES} retries"
  end

  if retries > 1
    @logger.info("#{logger_name}-failed-to-reconnect, will try again in #{retry_in} seconds...")
  end

  EM.add_timer(retry_in) { retry_reconnect }
end