class LitmusPaper::Metric::HaproxyBackendsHealth

Public Class Methods

new(weight, domain_socket, cluster, options = {}) click to toggle source
# File lib/litmus_paper/metric/haproxy_backends_health.rb, line 8
def initialize(weight, domain_socket, cluster, options = {})
  @weight = weight
  @domain_socket = domain_socket
  @cluster = cluster
  @timeout = options.fetch(:timeout_seconds, 2)
end

Public Instance Methods

current_health() click to toggle source
# File lib/litmus_paper/metric/haproxy_backends_health.rb, line 15
def current_health
  servers = find_servers(@domain_socket, @timeout, @cluster)

  up_weight = servers
    .select { |server| server["status"] == "UP" }
    .inject(0) { |sum, server| sum + server["weight"].to_f }

  total_weight = servers
    .inject(0) { |sum, server| sum + server["weight"].to_f }

  ((up_weight / total_weight) * @weight).to_i

rescue Timeout::Error
  LitmusPaper.logger.info("HAProxy available check timed out for #{@cluster}")
  0
rescue => e
  LitmusPaper.logger.info("HAProxy available check failed for #{@cluster} with #{e.message}")
  0
end
stats() click to toggle source
# File lib/litmus_paper/metric/haproxy_backends_health.rb, line 35
def stats
  {}
end
to_s() click to toggle source
# File lib/litmus_paper/metric/haproxy_backends_health.rb, line 39
def to_s
  "Metric::HaproxyBackendsHealth(#{@weight}, #{@cluster})"
end