Class: LoadBalancer::Algo
- Inherits:
-
Object
- Object
- LoadBalancer::Algo
- Includes:
- Healthcheck, RedisLua
- Defined in:
- lib/multi_dbs_load_balancer/load_balancer/algo.rb
Direct Known Subclasses
Hash, LeastConnection, LeastResponseTime, Randomized, RoundRobin, WeightRoundRobin
Instance Attribute Summary collapse
-
#database_configs ⇒ Object
readonly
Returns the value of attribute database_configs.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #after_connected ⇒ Object
- #after_executed ⇒ Object
- #connected_to_next_db(**options, &blk) ⇒ Object
- #fail_over(next_choices) ⇒ Object
-
#initialize(database_configs, redis:, key:) ⇒ Algo
constructor
A new instance of Algo.
- #next_db(**options) ⇒ Object
- #warm_up ⇒ Object
Methods included from Healthcheck
#db_available?, #mark_db_down, #mark_redis_down, #redis_available?
Methods included from RedisLua
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_configs ⇒ Object (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 |
#key ⇒ Object (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 |
#redis ⇒ Object (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_connected ⇒ Object
20 21 |
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 20 def after_connected end |
#after_executed ⇒ Object
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(**, &blk) candidate_db, db_index = next_db(**) raise LoadBalancer::AllDatabasesHaveDown if candidate_db.nil? if .has_key?(:bases) ::ActiveRecord::Base.connected_to_many(*[:bases], **candidate_db.slice(:shard, :role), prevent_writes: [:prevent_writes]) do after_connected blk.call end else ::ActiveRecord::Base.connected_to(**candidate_db.slice(:shard, :role), prevent_writes: [: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(**, &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
16 17 18 |
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 16 def next_db(**) raise NotImplementedError, "" end |
#warm_up ⇒ Object
13 14 |
# File 'lib/multi_dbs_load_balancer/load_balancer/algo.rb', line 13 def warm_up end |