Class: LoadBalancer::LeastResponseTime
- Defined in:
- lib/multi_dbs_load_balancer/load_balancer/least_response_time.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
Constructor Details
This class inherits a constructor from LoadBalancer::Algo
Instance Method Details
#after_connected ⇒ Object
37 38 39 |
# File 'lib/multi_dbs_load_balancer/load_balancer/least_response_time.rb', line 37 def after_connected @start_time = Time.now end |
#after_executed ⇒ Object
41 42 43 |
# File 'lib/multi_dbs_load_balancer/load_balancer/least_response_time.rb', line 41 def after_executed update_response_time(Time.now - @start_time) if @start_time end |
#next_db(**options) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/multi_dbs_load_balancer/load_balancer/least_response_time.rb', line 29 def next_db(**) @least_db_index = top_least.to_i return @database_configs[@least_db_index], @least_db_index if db_available?(@least_db_index) # fail over fail_over(next_dbs) end |
#warm_up ⇒ Object
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_response_time.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, 0, i) end end rescue => error mark_redis_down if error.is_a?(Redis::CannotConnectError) ensure return if @@response_times.has_key?(db_conns_pq_key) @@response_times[db_conns_pq_key] = MinHeap.new @database_configs.size.times do |i| @@response_times[db_conns_pq_key].push([0, i]) end end end end |