lib/wordnik/load_balancer.rb in wordnik-4.08 vs lib/wordnik/load_balancer.rb in wordnik-4.09

- old
+ new

@@ -17,12 +17,12 @@ attr_accessor :all_hosts attr_accessor :failed_hosts_table attr_accessor :current_host def initialize(hosts) - @all_hosts = hosts - @hosts = @all_hosts + @all_hosts = hosts.clone + @hosts = @all_hosts.clone @failed_hosts_table = {} @current_host = nil end def host @@ -34,11 +34,11 @@ def inform_failure #Wordnik.logger.debug "Informing failure about #{@current_host}. table: #{@failed_hosts_table.inspect}" if @failed_hosts_table.include?(@current_host) failures, failed_time = @failed_hosts_table[@current_host] - @failed_hosts_table[@current_host] = [failures+1, failed_time] + @failed_hosts_table[@current_host] = [failures+1, Time.now.to_f] else @failed_hosts_table[@current_host] = [1, Time.now.to_f] # failure count, first failure time end #Wordnik.logger.debug "Informed failure about #{@current_host}. table now: #{@failed_hosts_table.inspect}" @hosts.delete(@current_host) @@ -55,16 +55,26 @@ def restore_failed_hosts_maybe return if @failed_hosts_table.size == 0 @failed_hosts_table.each do |host, pair| failures, failed_time = pair - seconds_since_first_failure = (Time.now.to_f - failed_time) - #Wordnik.logger.debug "Seconds since #{host}'s first failure: #{seconds_since_first_failure} compared to #{2**(failures-1)}" + n = Time.now.to_f + seconds_since_last_failure = (n - failed_time) # exponential backoff, but try every hour... - if (seconds_since_first_failure > [3600, 2**(failures-1)].min) + if (seconds_since_last_failure > [3600, 2**(failures-1)].min) @hosts << host # give it a chance to succeed ... - #Wordnik.logger.debug "Added #{host} to @hosts; now: #{@hosts}" + update_failed_time(host, n) end + end + end + + # mostly useful in mock testing... + def update_failed_time(host, time=Time.now) + if @failed_hosts_table.include? host + failures, _ = @failed_hosts_table[host] + @failed_hosts_table[host] = [failures, time.to_f] + else + @failed_hosts_table[host] = [1,time.to_f] end end end end