class LitmusPaper::Metric::InternetHealth

Public Class Methods

new(weight, hosts, options = {}) click to toggle source
# File lib/litmus_paper/metric/internet_health.rb, line 4
def initialize(weight, hosts, options = {})
  @weight = weight
  @hosts = hosts
  @options = options
  @timeout = options.fetch(:timeout_seconds, 5) / @hosts.length.to_f
end

Public Instance Methods

current_health() click to toggle source
# File lib/litmus_paper/metric/internet_health.rb, line 25
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
stats() click to toggle source
# File lib/litmus_paper/metric/internet_health.rb, line 35
def stats
  {}
end
tcp_connect?(host, port) click to toggle source
# File lib/litmus_paper/metric/internet_health.rb, line 11
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
to_s() click to toggle source
# File lib/litmus_paper/metric/internet_health.rb, line 39
def to_s
  "Metric::InternetHealth(#{@weight}, #{@hosts.inspect}, #{@options.inspect})"
end