Class: LoadBalancer::LeastConnection

Inherits:
Algo
  • Object
show all
Defined in:
lib/multi_dbs_load_balancer/load_balancer/least_connection.rb

Instance Attribute Summary

Attributes inherited from Algo

#database_configs, #key, #redis

Instance Method Summary collapse

Methods inherited from Algo

#connected_to_next_db, #fail_over, #initialize

Methods included from Healthcheck

#db_available?, #mark_db_down, #mark_redis_down, #redis_available?

Methods included from RedisLua

eval_lua_script

Constructor Details

This class inherits a constructor from LoadBalancer::Algo

Instance Method Details

#after_connectedObject



37
38
39
# File 'lib/multi_dbs_load_balancer/load_balancer/least_connection.rb', line 37

def after_connected
    increase
end

#after_executedObject



41
42
43
# File 'lib/multi_dbs_load_balancer/load_balancer/least_connection.rb', line 41

def after_executed
    decrease
end

#next_db(**options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/multi_dbs_load_balancer/load_balancer/least_connection.rb', line 29

def next_db(**options)
    @least_db_index = top_least
    return @database_configs[@least_db_index], @least_db_index if db_available?(@least_db_index)
    
    # fail over
    fail_over(next_dbs)
end

#warm_upObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/multi_dbs_load_balancer/load_balancer/least_connection.rb', line 8

def warm_up
    pq_mutex.synchronize do
        begin
            unless @redis.exists?(db_conns_pq_key)
                @database_configs.size.times do |i|
                    @redis.zincrby(db_conns_pq_key, 1, i)
                end
            end
        rescue => e
            # p e
        ensure
            return if @@leasts.has_key?(db_conns_pq_key)

            @@leasts[db_conns_pq_key] = MinHeap.new
            @database_configs.size.times do |i|
                @@leasts[db_conns_pq_key].push([1, i])
            end
        end
    end
end