lib/elasticsearch/transport/transport/base.rb in elasticsearch-transport-1.0.14 vs lib/elasticsearch/transport/transport/base.rb in elasticsearch-transport-1.0.15
- old
+ new
@@ -45,10 +45,11 @@
@last_request_at = Time.now
@reload_connections = options[:reload_connections]
@reload_after = options[:reload_connections].is_a?(Fixnum) ? options[:reload_connections] : DEFAULT_RELOAD_AFTER
@resurrect_after = options[:resurrect_after] || DEFAULT_RESURRECT_AFTER
@max_retries = options[:retry_on_failure].is_a?(Fixnum) ? options[:retry_on_failure] : DEFAULT_MAX_RETRIES
+ @retry_on_status = Array(options[:retry_on_status]).map { |d| d.to_i }
end
# Returns a connection from the connection pool by delegating to {Connections::Collection#get_connection}.
#
# Resurrects dead connection if the `resurrect_after` timeout has passed.
@@ -93,13 +94,22 @@
# @api private
#
def __rebuild_connections(arguments={})
@hosts = arguments[:hosts] || []
@options = arguments[:options] || {}
+ __close_connections
@connections = __build_connections
end
+ # Closes the connections collection.
+ #
+ # @api private
+ #
+ def __close_connections
+ # to be implemented by specific transports
+ end
+
# Log request and response information.
#
# @api private
#
def __log(method, path, params, body, url, response, json, took, duration)
@@ -190,10 +200,27 @@
response = block.call(connection, url)
connection.healthy! if connection.failures > 0
+ # Raise an exception so we can catch it for `retry_on_status`
+ __raise_transport_error(response) if response.status.to_i >= 300 && @retry_on_status.include?(response.status.to_i)
+
+ rescue Elasticsearch::Transport::Transport::ServerError => e
+ if @retry_on_status.include?(response.status)
+ logger.warn "[#{e.class}] Attempt #{tries} to get response from #{url}" if logger
+ logger.debug "[#{e.class}] Attempt #{tries} to get response from #{url}" if logger
+ if tries <= max_retries
+ retry
+ else
+ logger.fatal "[#{e.class}] Cannot get response from #{url} after #{tries} tries" if logger
+ raise e
+ end
+ else
+ raise e
+ end
+
rescue *host_unreachable_exceptions => e
logger.error "[#{e.class}] #{e.message} #{connection.host.inspect}" if logger
connection.dead!
@@ -215,10 +242,11 @@
end
rescue Exception => e
logger.fatal "[#{e.class}] #{e.message} (#{connection.host.inspect if connection})" if logger
raise e
- end
+
+ end #/begin
duration = Time.now-start if logger || tracer
if response.status.to_i >= 300
__log method, path, params, body, url, response, nil, 'N/A', duration if logger