class Bosh::Monitor::TcpConnection

Constants

BACKOFF_CEILING
MAX_RETRIES

Attributes

logger_name[R]
retries[R]

Public Class Methods

new(logger_name, host, port) click to toggle source
# 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

Public Instance Methods

connection_completed() click to toggle source
# 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_retries() click to toggle source
# File lib/bosh/monitor/protocols/tcp_connection.rb, line 21
def increment_retries
  @retries += 1
end
receive_data(data) click to toggle source
# File lib/bosh/monitor/protocols/tcp_connection.rb, line 57
def receive_data(data)
  @logger.info("#{logger_name} << #{data.chomp}")
end
reset_retries() click to toggle source
# File lib/bosh/monitor/protocols/tcp_connection.rb, line 17
def reset_retries
  @retries = 0
end
retry_reconnect() click to toggle source
# File lib/bosh/monitor/protocols/tcp_connection.rb, line 52
def retry_reconnect
  @logger.info("#{@logger_name}-reconnecting (#{retries})...")
  reconnect(@host, @port)
end
unbind() click to toggle source
# 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