This file is basically a copy and paste from the tsdb connection. It would be good to refactor this, so that they use a common parent class and to reduce code duplication.

module Collector
  # TSDB connection for sending metrics
  class Connection < EventMachine::Connection
    def initialize(logger_name)
      @logger_name = logger_name
    end

    def connection_completed
      Config.logger.info("#{@logger_name}-connected")
      @port, @ip = Socket.unpack_sockaddr_in(get_peername)
    end

    def unbind
      if @port && @ip
        Config.logger.warn("#{@logger_name}-connection-lost")
        EM.add_timer(1.0) do
          begin
            reconnect(@ip, @port)
          rescue EventMachine::ConnectionError => e
            Config.logger.warn("#{@logger_name}-connection-error", error: e, backtrace: e.backtrace)
            unbind
          end
        end
      else
        Config.logger.fatal("#{@logger_name}.could-not-connect")
        exit!
      end
    end

    def receive_data(_)
    end
  end
end


require_relative "collection"

module Collector
  # TSDB connection for sending metrics
  class GraphiteConnection < Collector::Connection
    def initialize
      super("collector.graphite")
    end
  end
end