Sha256: 13802aa650138716e0afa15ef7321a43bcc6dfe828ff4350d2590c67a2343161

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'litmus_paper/haproxy_util'

module LitmusPaper
  module Metric
    class HaproxyBackendsHealth
      include LitmusPaper::HaproxyUtil

      def initialize(weight, domain_socket, cluster, options = {})
        @weight = weight
        @domain_socket = domain_socket
        @cluster = cluster
        @timeout = options.fetch(:timeout_seconds, 2)
      end

      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

      def stats
        {}
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
litmus_paper-1.6.2 lib/litmus_paper/metric/haproxy_backends_health.rb
litmus_paper-1.6.1 lib/litmus_paper/metric/haproxy_backends_health.rb