Class: Elasticsearch::Transport::Transport::Connections::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/transport/transport/connections/connection.rb

Overview

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).

Constant Summary collapse

DEFAULT_RESURRECT_TIMEOUT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • arguments (Hash) (defaults to: {})

    a customizable set of options

Options Hash (arguments):

  • :host (Hash)

    Host information (example: ‘’localhost’, port: 9200‘)

  • :connection (Object)

    The transport-specific physical connection or “session”

  • :options (Hash)

    Options (usually passed in from transport)



42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



35
36
37
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 35

def connection
  @connection
end

#dead_sinceObject (readonly)

Returns the value of attribute dead_since.



35
36
37
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 35

def dead_since
  @dead_since
end

#failuresObject (readonly)

Returns the value of attribute failures.



35
36
37
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 35

def failures
  @failures
end

#hostObject (readonly)

Returns the value of attribute host.



35
36
37
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 35

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 35

def options
  @options
end

#verifiedObject

Returns the value of attribute verified.



36
37
38
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 36

def verified
  @verified
end

Instance Method Details

#==(other) ⇒ Boolean

Equality operator based on connection protocol, host, port and attributes

Returns:

  • (Boolean)


145
146
147
148
149
150
# 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

#alive!self

Marks this connection as alive, ie. it is eligible to be returned from the pool by the selector.

Returns:

  • (self)


102
103
104
105
106
107
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 102

def alive!
  @state_mutex.synchronize do
    @dead     = false
  end
  self
end

#dead!self

Marks this connection as dead, incrementing the ‘failures` counter and storing the current time as `dead_since`.

Returns:

  • (self)


89
90
91
92
93
94
95
96
# 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

#dead?Boolean

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

Returns:

  • (Boolean)


80
81
82
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 80

def dead?
  @dead || false
end

#full_path(path, params = {}) ⇒ String

Returns the complete endpoint path with serialized parameters.

Returns:

  • (String)


72
73
74
# 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

#full_url(path, params = {}) ⇒ String

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

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
# 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

#healthy!self

Marks this connection as healthy, ie. a request has been successfully performed with it.

Returns:

  • (self)


113
114
115
116
117
118
119
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 113

def healthy!
  @state_mutex.synchronize do
    @dead     = false
    @failures = 0
  end
  self
end

#resurrect!self?

Marks this connection as alive, if the required timeout has passed.

Returns:

  • (self, nil)

See Also:



127
128
129
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 127

def resurrect!
  alive! if resurrectable?
end

#resurrectable?Boolean

Returns true if the connection is eligible to be resurrected as alive, false otherwise.

Returns:

  • (Boolean)


135
136
137
138
139
# 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

#to_sString

Returns:

  • (String)


154
155
156
# 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