Sha256: 92f0bda75ac52a6c8e8710f1869c4f62d009f5a58a3935a47db3cdb0d4f70988

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module EbisuConnection
  class SlaveGroup
    class AllSlavesHasGoneError < StandardError; end

    def initialize(slaves_conf, slave_group)
      @slaves = slaves_conf.map do |conf|
        Slave.new(conf, slave_group)
      end

      recalc_roulette
    end

    def sample
      raise AllSlavesHasGoneError if @slaves.empty?
      @slaves[@roulette.sample]
    end

    def recovery_connection?
      dead_slaves = @slaves.select{|s| !s.active? }
      return false if dead_slaves.empty?

      dead_slaves.each do |s|
        s.disconnect!
        @slaves.delete(s)
      end

      raise AllSlavesHasGoneError if @slaves.empty?

      recalc_roulette
      true
    end

    def all_disconnect!
      @reserve_release = nil
      @slaves.each {|s| s.disconnect!}
    end

    def reserve_release_connection!
      @reserve_release = true
    end

    def reserved_release?
      !!@reserve_release
    end

    private

    def recalc_roulette
      weight_list = @slaves.map {|s| s.weight }

      @roulette = []
      gcd = GreatestCommonDivisor.calc(weight_list)
      weight_list.each_with_index do |w, index|
        weight = w / gcd
        @roulette.concat([index] * weight)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ebisu_connection-2.1.0 lib/ebisu_connection/slave_group.rb
ebisu_connection-2.0.0 lib/ebisu_connection/slave_group.rb