Sha256: 66633577d33ce18b432947dec30f79f091b9f5f3c97164e9f3d993f2da793550

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 KB

Contents

module LitmusPaper
  module Metric
    class InternetHealth
      def initialize(weight, hosts, options = {})
        @weight = weight
        @hosts = hosts
        @options = options
        @timeout = options.fetch(:timeout_seconds, 5) / @hosts.length.to_f
      end

      def tcp_connect?(host, port)
        Timeout.timeout(@timeout) do
          socket = TCPSocket.new(host, port)
          socket.close
        end
        true
      rescue Timeout::Error
        LitmusPaper.logger.info("Timed out connecting to #{host}:#{port} after #{@timeout}s")
        false
      rescue => e
        LitmusPaper.logger.info("TCP connect to #{host}:#{port} failed with #{e.message}")
        false
      end

      def current_health
        health = @weight * @hosts.reduce(Rational(0)) do |memo, host|
          if tcp_connect?(*host.split(':'))
            memo += Rational(1) / Rational(@hosts.length)
          end
          memo
        end
        health.to_i
      end

      def to_s
        "Metric::InternetHealth(#{@weight}, #{@hosts.inspect}, #{@options.inspect})"
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
litmus_paper-1.4.2 lib/litmus_paper/metric/internet_health.rb
litmus_paper-1.4.1 lib/litmus_paper/metric/internet_health.rb
litmus_paper-1.3.0 lib/litmus_paper/metric/internet_health.rb
litmus_paper-1.2.0 lib/litmus_paper/metric/internet_health.rb
litmus_paper-1.1.1 lib/litmus_paper/metric/internet_health.rb
litmus_paper-1.1.0 lib/litmus_paper/metric/internet_health.rb
litmus_paper-1.0.0 lib/litmus_paper/metric/internet_health.rb
litmus_paper-0.9.9 lib/litmus_paper/metric/internet_health.rb
litmus_paper-0.9.7 lib/litmus_paper/metric/internet_health.rb
litmus_paper-0.9.6 lib/litmus_paper/metric/internet_health.rb