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

connection[R]
dead_since[R]
failures[R]
host[R]
options[R]

Public Class Methods

new(arguments={}) click to toggle source

@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 25
def initialize(arguments={})
  @host       = arguments[:host]
  @connection = arguments[:connection]
  @options    = arguments[:options] || {}
  @state_mutex = Mutex.new

  @options[:resurrect_timeout] ||= DEFAULT_RESURRECT_TIMEOUT
  @dead = false
  @failures = 0
end

Public Instance Methods

==(other) click to toggle source

Equality operator based on connection protocol, host and port

@return [Boolean]

# File lib/elasticsearch/transport/transport/connections/connection.rb, line 125
def ==(other)
  self.host[:protocol] == other.host[:protocol] && \
  self.host[:host] == other.host[:host] && \
  self.host[:port].to_i == other.host[:port].to_i
end
alive!() click to toggle source

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 82
def alive!
  @state_mutex.synchronize do
    @dead     = false
  end
  self
end
dead!() click to toggle source

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 69
def dead!
  @state_mutex.synchronize do
    @dead       = true
    @failures  += 1
    @dead_since = Time.now
  end
  self
end
dead?() click to toggle source

Returns true when this connection has been marked dead, false otherwise.

@return [Boolean]

# File lib/elasticsearch/transport/transport/connections/connection.rb, line 60
def dead?
  @dead || false
end
full_path(path, params={}) click to toggle source

Returns the complete endpoint path with serialized parameters.

@return [String]

# File lib/elasticsearch/transport/transport/connections/connection.rb, line 52
def full_path(path, params={})
  path + (params.empty? ? '' : "?#{::Faraday::Utils::ParamsHash[params].to_query}")
end
full_url(path, params={}) click to toggle source

Returns the complete endpoint URL with host, port, path and serialized parameters.

@return [String]

# File lib/elasticsearch/transport/transport/connections/connection.rb, line 40
def full_url(path, params={})
  url  = "#{host[:protocol]}://"
  url += "#{host[:user]}:#{host[:password]}@" if host[:user]
  url += "#{host[:host]}:#{host[:port]}"
  url += "#{host[:path]}" if host[:path]
  url += "/#{full_path(path, params)}"
end
healthy!() click to toggle source

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 93
def healthy!
  @state_mutex.synchronize do
    @dead     = false
    @failures = 0
  end
  self
end
resurrect!() click to toggle source

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 107
def resurrect!
  alive! if resurrectable?
end
resurrectable?() click to toggle source

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 115
def resurrectable?
  @state_mutex.synchronize {
    Time.now > @dead_since + ( @options[:resurrect_timeout] * 2 ** (@failures-1) )
  }
end
to_s() click to toggle source

@return [String]

# File lib/elasticsearch/transport/transport/connections/connection.rb, line 133
def to_s
  "<#{self.class.name} host: #{host} (#{dead? ? 'dead since ' + dead_since.to_s : 'alive'})>"
end