vendor/bundle/jruby/2.3.0/gems/elasticsearch-transport-5.0.4/lib/elasticsearch/transport/transport/base.rb
# Performs a request to Elasticsearch, while handling logging, tracing, marking dead connections,
# retrying the request and reloading the connections.
#
# @abstract The transport implementation has to implement this method either in full,
#           or by invoking this method with a block. See {HTTP::Faraday#perform_request} for an example.
#
# @param method [String] Request method
# @param path   [String] The API endpoint
# @param params [Hash]   Request parameters (will be serialized by {Connections::Connection#full_url})
# @param body   [Hash]   Request body (will be serialized by the {#serializer})
# @param block  [Proc]   Code block to evaluate, passed from the implementation
#
# @return [Response]
# @raise  [NoMethodError] If no block is passed
# @raise  [ServerError]   If request failed on server
# @raise  [Error]         If no connection is available
#
Performs a request to Elasticsearch, while handling logging, tracing, marking dead connections,
retrying the request and reloading the connections.
Performs a request to Elasticsearch while handling logging tracing marking dead connections retrying the request and reloading the connections .
def perform_request(method, path, params={}, body=nil, &block)
          raise NoMethodError, "Implement this method in your transport class" unless block_given?
          start = Time.now if logger || tracer
          tries = 0
          params = params.clone
          ignore = Array(params.delete(:ignore)).compact.map { |s| s.to_i }
          begin
            tries     += 1
            connection = get_connection or raise Error.new("Cannot get new connection from pool.")
            if connection.connection.respond_to?(:params) && connection.connection.params.respond_to?(:to_hash)
              params = connection.connection.params.merge(params.to_hash)
            end
            url        = connection.full_url(path, params)
            response   = block.call(connection, url)
            connection.healthy! if connection.failures > 0
            __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
              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!
            if @options[:reload_on_failure] and tries < connections.all.size
              logger.warn "[#{e.class}] Reloading connections (attempt #{tries} of #{connections.all.size})" if logger
              reload_connections! and retry
            end
            if @options[:retry_on_failure]
              logger.warn "[#{e.class}] Attempt #{tries} connecting to #{connection.host.inspect}" if logger
              if tries <= max_retries
                retry
              else
                logger.fatal "[#{e.class}] Cannot connect to #{connection.host.inspect} after #{tries} tries" if logger
                raise e
              end
            else
              raise e
            end
          rescue Exception => e
            logger.fatal "[#{e.class}] #{e.message} (#{connection.host.inspect if connection})" if logger
            raise e
          end 
          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
            __trace  method, path, params, body, url, response, nil, 'N/A', duration if tracer
            __log_failed response if logger && !ignore.include?(response.status.to_i)
            __raise_transport_error response unless ignore.include?(response.status.to_i)
          end
          json     = serializer.load(response.body) if response.body && !response.body.empty? && response.headers && response.headers["content-type"] =~ /json/
          took     = (json['took'] ? sprintf('%.3fs', json['took']/1000.0) : 'n/a') rescue 'n/a' if logger || tracer
          __log   method, path, params, body, url, response, json, took, duration if logger && !ignore.include?(response.status.to_i)
          __trace method, path, params, body, url, response, json, took, duration if tracer
          Response.new response.status, json || response.body, response.headers
        ensure
          @last_request_at = Time.now
        end
def perform_request ( method , path , params = { } , body = nil , & block ) raise NoMethodError , "Implement this method in your transport class" unless block_given? start = Time . now if logger || tracer tries = 0 params = params . clone ignore = Array ( params . delete ( :ignore ) ) . compact . map { | s | s . to_i } begin tries += 1 connection = get_connection or raise Error . new ( "Cannot get new connection from pool." ) if connection . connection . respond_to? ( :params ) && connection . connection . params . respond_to? ( :to_hash ) params = connection . connection . params . merge ( params . to_hash ) end url = connection . full_url ( path , params ) response = block . call ( connection , url ) connection . healthy! if connection . failures > 0 __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 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! if @options [ :reload_on_failure ] and tries < connections . all . size logger . warn "[#{e.class}] Reloading connections (attempt #{tries} of #{connections.all.size})" if logger reload_connections! and retry end if @options [ :retry_on_failure ] logger . warn "[#{e.class}] Attempt #{tries} connecting to #{connection.host.inspect}" if logger if tries <= max_retries retry else logger . fatal "[#{e.class}] Cannot connect to #{connection.host.inspect} after #{tries} tries" if logger raise e end else raise e end rescue Exception => e logger . fatal "[#{e.class}] #{e.message} (#{connection.host.inspect if connection})" if logger raise e end 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 __trace method , path , params , body , url , response , nil , 'N/A' , duration if tracer __log_failed response if logger && ! ignore . include? ( response . status . to_i ) __raise_transport_error response unless ignore . include? ( response . status . to_i ) end json = serializer . load ( response . body ) if response . body && ! response . body . empty? && response . headers && response . headers [ "content-type" ] =~ / json / took = ( json [ 'took' ] ? sprintf ( '%.3fs' , json [ 'took' ] / 1000.0 ) : 'n/a' ) rescue 'n/a' if logger || tracer __log method , path , params , body , url , response , json , took , duration if logger && ! ignore . include? ( response . status . to_i ) __trace method , path , params , body , url , response , json , took , duration if tracer Response . new response . status , json || response . body , response . headers ensure @last_request_at = Time . now end
Performs a request to Elasticsearch, while handling logging, tracing, marking dead connections,
retrying the request and reloading the connections.
Performs a request to Elasticsearch while handling logging tracing marking dead connections retrying the request and reloading the connections .
# Raise an exception so we can catch it for `retry_on_status` #/begin # Log the failure only when `ignore` doesn't match the response status
None
Response
[NoMethodError] If no block is passed
[ NoMethodError ] If no block is passed
[ServerError] If request failed on server
[ ServerError ] If request failed on server
[Error] If no connection is available
[ Error ] If no connection is available
method
None
path
The API endpoint
The API endpoint
params
Request parameters (will be serialized by {Connections::Connection#full_url})
Request parameters ( will be serialized by { Connections :: Connection#full_url } )
body
Request body (will be serialized by the {#serializer})
Request body ( will be serialized by the { #serializer } )
block
Code block to evaluate, passed from the implementation
Code block to evaluate passed from the implementation
abstract
The transport implementation has to implement this method either in full, or by invoking this method with a block. See {HTTP::Faraday#perform_request} for an example.
The transport implementation has to implement this method either in full or by invoking this method with a block . See { HTTP :: Faraday#perform_request } for an example .