<commit_before># $LICENSE
# Copyright 2013-2014 Spotify AB. All rights reserved.
#
# The contents of this file are licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

require 'eventmachine'

require_relative '../../reporter'
require_relative '../../retrier'

module FFWD::TCP
  class Bind
    include FFWD::Reporter

    # Default initial timeout when binding fails.
    DEFAULT_REBIND_TIMEOUT = 10

    def self.prepare opts
      opts[:rebind_timeout] ||= DEFAULT_REBIND_TIMEOUT
      opts
    end

    report_meta :protocol => :tcp, :direction => :input

    report_key :failed_events, :meta => {:what => "failed-events", :unit => :event}
    report_key :received_events, :meta => {:what => "received-events", :unit => :event}

    report_key :failed_metrics, :meta => {:what => :failed_metrics, :unit => :metric}
    report_key :received_metrics, :meta => {:what => :received_metrics, :unit => :metric}

    attr_reader :log, :reporter_meta

    def initialize core, log, host, port, connection, config
      @log = log
      @peer = "#{host}:#{port}"
      @reporter_meta = {:component => connection.plugin_type, :listen => @peer}

      @server = nil

      info = "tcp://#{@peer}"
      rebind_timeout = config[:rebind_timeout]

      r = FFWD.retry :timeout => rebind_timeout do |a|
        @server = EM.start_server host, port, connection, self, core, config

        log.info "Bind on #{info} (attempt #{a})"
        log.info "  config: #{config.inspect}"
      end

      r.error do |a, t, e|
        log.warning "Bind on #{info} failed, retry ##{a} in #{t}s: #{e}"
      end

      r.depend_on core

      core.stopping do
        if @server
          EM.stop_server @server<commit_msg>[core] fix statistics failed_metrics -> failed-metrics<commit_after># $LICENSE
# Copyright 2013-2014 Spotify AB. All rights reserved.
#
# The contents of this file are licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

require 'eventmachine'

require_relative '../../reporter'
require_relative '../../retrier'

module FFWD::TCP
  class Bind
    include FFWD::Reporter

    # Default initial timeout when binding fails.
    DEFAULT_REBIND_TIMEOUT = 10

    def self.prepare opts
      opts[:rebind_timeout] ||= DEFAULT_REBIND_TIMEOUT
      opts
    end

    report_meta :protocol => :tcp, :direction => :input

    report_key :failed_events, :meta => {:what => "failed-events", :unit => :event}
    report_key :received_events, :meta => {:what => "received-events", :unit => :event}

    report_key :failed_metrics, :meta => {:what => "failed-metrics", :unit => :metric}
    report_key :received_metrics, :meta => {:what => "received-metrics", :unit => :metric}

    attr_reader :log, :reporter_meta

    def initialize core, log, host, port, connection, config
      @log = log
      @peer = "#{host}:#{port}"
      @reporter_meta = {:component => connection.plugin_type, :listen => @peer}

      @server = nil

      info = "tcp://#{@peer}"
      rebind_timeout = config[:rebind_timeout]

      r = FFWD.retry :timeout => rebind_timeout do |a|
        @server = EM.start_server host, port, connection, self, core, config

        log.info "Bind on #{info} (attempt #{a})"
        log.info "  config: #{config.inspect}"
      end

      r.error do |a, t, e|
        log.warning "Bind on #{info} failed, retry ##{a} in #{t}s: #{e}"
      end

      r.depend_on core

      core.stopping do
        if @server
          EM.stop_server @server
{'fraction_non_alphanumeric': 0.1223705044107668, 'fraction_numerical': 0.006333408731056322, 'mean_word_length': 3.2275334608030595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3490832', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1282, 'n_words': 457}