Class: LitmusPaper::Metric::HaproxyBackendsHealth

Inherits:
Object
  • Object
show all
Includes:
HaproxyUtil
Defined in:
lib/litmus_paper/metric/haproxy_backends_health.rb

Instance Method Summary collapse

Methods included from HaproxyUtil

#_fetch_stats, #_get_stats, #_parse_stats, #find_backend, #find_servers

Constructor Details

#initialize(weight, domain_socket, cluster, options = {}) ⇒ HaproxyBackendsHealth

Returns a new instance of HaproxyBackendsHealth.



8
9
10
11
12
13
# 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

Instance Method Details

#current_healthObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

#statsObject



35
36
37
# File 'lib/litmus_paper/metric/haproxy_backends_health.rb', line 35

def stats
  {}
end

#to_sObject



39
40
41
# File 'lib/litmus_paper/metric/haproxy_backends_health.rb', line 39

def to_s
  "Metric::HaproxyBackendsHealth(#{@weight}, #{@cluster})"
end