class Elasticsearch::Transport::Transport::Connections::Connection
Wraps the connection information and logic.
The Connection
instance wraps the host information (hostname, port, attributes, etc), as well as the “session” (a transport client object, such as a {HTTP::Faraday} instance).
It provides methods to construct and properly encode the URLs and paths for passing them to the transport client object.
It provides methods to handle connection livecycle (dead, alive, healthy).
Constants
- DEFAULT_RESURRECT_TIMEOUT
Attributes
Public Class Methods
@option arguments [Hash] :host Host information (example: ‘{host: ’localhost’, port: 9200}‘) @option arguments [Object] :connection The transport-specific physical connection or “session” @option arguments [Hash] :options Options (usually passed in from transport)
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 42 def initialize(arguments={}) @host = arguments[:host].is_a?(Hash) ? Redacted.new(arguments[:host]) : arguments[:host] @connection = arguments[:connection] @options = arguments[:options] || {} @verified = false @state_mutex = Mutex.new @options[:resurrect_timeout] ||= DEFAULT_RESURRECT_TIMEOUT @dead = false @failures = 0 end
Public Instance Methods
Equality operator based on connection protocol, host, port and attributes
@return [Boolean]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 145 def ==(other) self.host[:protocol] == other.host[:protocol] && \ self.host[:host] == other.host[:host] && \ self.host[:port].to_i == other.host[:port].to_i && \ self.host[:attributes] == other.host[:attributes] end
Marks this connection as alive, ie. it is eligible to be returned from the pool by the selector.
@return [self]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 102 def alive! @state_mutex.synchronize do @dead = false end self end
Marks this connection as dead, incrementing the ‘failures` counter and storing the current time as `dead_since`.
@return [self]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 89 def dead! @state_mutex.synchronize do @dead = true @failures += 1 @dead_since = Time.now end self end
Returns true when this connection has been marked dead, false otherwise.
@return [Boolean]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 80 def dead? @dead || false end
Returns the complete endpoint path with serialized parameters.
@return [String]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 72 def full_path(path, params={}) path + (params.empty? ? '' : "?#{::Faraday::Utils::ParamsHash[params].to_query}") end
Returns the complete endpoint URL with host, port, path and serialized parameters.
@return [String]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 58 def full_url(path, params = {}) url = "#{host[:protocol]}://" url += "#{CGI.escape(host[:user])}:#{CGI.escape(host[:password])}@" if host[:user] url += "#{host[:host]}:#{host[:port]}" url += "#{host[:path]}" if host[:path] full_path = full_path(path, params) url += '/' unless full_path.match?(/^\//) url += full_path end
Marks this connection as healthy, ie. a request has been successfully performed with it.
@return [self]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 113 def healthy! @state_mutex.synchronize do @dead = false @failures = 0 end self end
Marks this connection as alive, if the required timeout has passed.
@return [self,nil] @see DEFAULT_RESURRECT_TIMEOUT
@see resurrectable?
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 127 def resurrect! alive! if resurrectable? end
Returns true if the connection is eligible to be resurrected as alive, false otherwise.
@return [Boolean]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 135 def resurrectable? @state_mutex.synchronize { Time.now > @dead_since + ( @options[:resurrect_timeout] * 2 ** (@failures-1) ) } end
@return [String]
# File lib/elasticsearch/transport/transport/connections/connection.rb, line 154 def to_s "<#{self.class.name} host: #{host} (#{dead? ? 'dead since ' + dead_since.to_s : 'alive'})>" end