Sha256: 741c8a9be549e63f25909867c1cebc4e76d60cb185efde5c38064efabcbd008b

Contents?: true

Size: 575 Bytes

Versions: 2

Compression:

Stored size: 575 Bytes

Contents

require "ebisu_connection/greatest_common_divisor"

module EbisuConnection
  class LoadBalancer
    def initialize(replicas)
      @replicas = replicas
    end

    def replica
      @replicas[roulette.sample]
    end

    private

    def roulette
      @roulette ||= calc_roulette
    end

    def calc_roulette
      set = []
      weight_list = @replicas.map(&:weight)
      gcd = GreatestCommonDivisor.calc(weight_list)

      weight_list.each_with_index do |w, index|
        weight = w / gcd
        set.concat([index] * weight)
      end

      set
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ebisu_connection-3.1.0 lib/ebisu_connection/load_balancer.rb
ebisu_connection-3.0.0 lib/ebisu_connection/load_balancer.rb