Class: LoadBalancer::Algo

Inherits:
Object
  • Object
show all
Includes:
Healthcheck, RedisLua
Defined in:
lib/multi_dbs_load_balancer/load_balancer/algo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Healthcheck

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

Methods included from RedisLua

eval_lua_script

Constructor Details

#initialize(database_configs, redis:, key:) ⇒ Algo

Returns a new instance of Algo.



7
8
9
10
11
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 7

def initialize(database_configs, redis:, key:)
    @database_configs = database_configs
    @redis = redis
    @key = key
end

Instance Attribute Details

#database_configsObject (readonly)

Returns the value of attribute database_configs.



5
6
7
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 5

def database_configs
  @database_configs
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 5

def key
  @key
end

#redisObject (readonly)

Returns the value of attribute redis.



5
6
7
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 5

def redis
  @redis
end

Instance Method Details

#after_connectedObject



20
21
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 20

def after_connected
end

#after_executedObject



23
24
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 23

def after_executed
end

#connected_to_next_db(**options, &blk) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 26

def connected_to_next_db(**options, &blk)
    candidate_db, db_index = next_db(**options)
    raise LoadBalancer::AllDatabasesHaveDown if candidate_db.nil?

    if options.has_key?(:bases)
        ::ActiveRecord::Base.connected_to_many(*options[:bases], **candidate_db.slice(:shard, :role), prevent_writes: options[:prevent_writes]) do
            after_connected
            blk.call
        end
    else
        ::ActiveRecord::Base.connected_to(**candidate_db.slice(:shard, :role), prevent_writes: options[:prevent_writes]) do
            after_connected
            blk.call
        end
    end
rescue ActiveRecord::ConnectionNotEstablished
    mark_db_down(db_index)
    @should_retry = true
ensure
    after_executed
    if @should_retry
        @should_retry = false
        connected_to_next_db(**options, &blk)
    end
end

#fail_over(next_choices) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 52

def fail_over(next_choices)
    candidate = next_choices.find do |i| 
        db_available?(i) 
    end

    if candidate
        [@database_configs[candidate], candidate]
    else
        [nil, -1]
    end
end

#next_db(**options) ⇒ Object

Raises:

  • (NotImplementedError)


16
17
18
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 16

def next_db(**options)
    raise NotImplementedError, ""
end

#warm_upObject



13
14
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 13

def warm_up
end