Class: LoadBalancedRestClient::LoadBalancer
- Inherits:
-
Object
- Object
- LoadBalancedRestClient::LoadBalancer
- Defined in:
- lib/load_balancer.rb
Defined Under Namespace
Classes: MaxTriesReached
Instance Attribute Summary collapse
-
#cluster ⇒ Object
Returns the value of attribute cluster.
Instance Method Summary collapse
-
#initialize(cluster, options = {}) ⇒ LoadBalancer
constructor
A new instance of LoadBalancer.
- #with_server(&req_blk) ⇒ Object
Constructor Details
#initialize(cluster, options = {}) ⇒ LoadBalancer
Returns a new instance of LoadBalancer.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/load_balancer.rb', line 6 def initialize(cluster, = {}) @cluster = cluster @human_readable_cluster = cluster.servers_to_try.join(", ") @exceptions_to_catch = [:catch] || [Errno::ECONNREFUSED, Errno::EHOSTUNREACH, RestClient::ServerBrokeConnection, RestClient::RequestTimeout] @max_tries = [:max_tries] || 4 @logger = [:logger] || Logger.new(STDOUT) @logger.info "Setting up load balancing: #{@human_readable_cluster}" end |
Instance Attribute Details
#cluster ⇒ Object
Returns the value of attribute cluster.
4 5 6 |
# File 'lib/load_balancer.rb', line 4 def cluster @cluster end |
Instance Method Details
#with_server(&req_blk) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/load_balancer.rb', line 17 def with_server(&req_blk) @try_counter = 0 result = false until result or @try_counter == @max_tries do @try_counter += 1 @server = @cluster.next_server @cluster.balance! @logger.info "#{human_readable_request_counter}: Trying #{@server}" result = try_request(&req_blk) end if result result else @logger.error "Max tries reached" raise MaxTriesReached.new("Max tries reached") end end |