lib/wordnik/load_balancer.rb in wordnik-4.09 vs lib/wordnik/load_balancer.rb in wordnik-4.10
- old
+ new
@@ -21,10 +21,11 @@
def initialize(hosts)
@all_hosts = hosts.clone
@hosts = @all_hosts.clone
@failed_hosts_table = {}
@current_host = nil
+ Wordnik.logger.info "LoadBalancer: Creating a load balancer using the following hosts: #{@hosts.join(', ')}"
end
def host
@current_host = hosts.first
@hosts.rotate!
@@ -41,17 +42,23 @@
@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)
@hosts = [@current_host] if @hosts.size == 0 # got to have something!
+ if @hosts == [@current_host]
+ Wordnik.logger.warn "LoadBalancer: host #{@current_host} failed, but it is the only remaining host. Not removing."
+ else
+ Wordnik.logger.info "LoadBalancer: host #{@current_host} failed. Removed from active hosts, which are now: #{@hosts.join(', ')}"
+ end
end
# success here means just that a successful connection was made
# and the website didn't time out.
def inform_success
@failed_hosts_table.delete(@current_host)
@hosts << @current_host unless @hosts.include? @current_host
+ Wordnik.logger.info "LoadBalancer: host #{@current_host} is working again, and has been added to active hosts, which are now: #{@hosts.join(', ')}"
@hosts
end
def restore_failed_hosts_maybe
return if @failed_hosts_table.size == 0
@@ -60,9 +67,10 @@
n = Time.now.to_f
seconds_since_last_failure = (n - failed_time)
# exponential backoff, but try every hour...
if (seconds_since_last_failure > [3600, 2**(failures-1)].min)
@hosts << host # give it a chance to succeed ...
+ Wordnik.logger.info "LoadBalancer: timeout for host #{host} failure exceeded; returning to active hosts, which are now: #{@hosts.join(', ')}"
update_failed_time(host, n)
end
end
end