Class: Elasticsearch::Transport::Transport::Connections::Connection
- Inherits:
-
Object
- Object
- Elasticsearch::Transport::Transport::Connections::Connection
- 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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#dead_since ⇒ Object
readonly
Returns the value of attribute dead_since.
-
#failures ⇒ Object
readonly
Returns the value of attribute failures.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#alive! ⇒ self
Marks this connection as alive, ie.
-
#dead! ⇒ self
Marks this connection as dead, incrementing the ‘failures` counter and storing the current time as `dead_since`.
-
#dead? ⇒ Boolean
Returns true when this connection has been marked dead, false otherwise.
-
#full_path(path, params = {}) ⇒ String
Returns the complete endpoint path with serialized parameters.
-
#full_url(path, params = {}) ⇒ String
Returns the complete endpoint URL with host, port, path and serialized parameters.
-
#healthy! ⇒ self
Marks this connection as healthy, ie.
-
#initialize(arguments = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#resurrect! ⇒ self?
Marks this connection as alive, if the required timeout has passed.
-
#resurrectable? ⇒ Boolean
Returns true if the connection is eligible to be resurrected as alive, false otherwise.
- #to_s ⇒ String
Constructor Details
#initialize(arguments = {}) ⇒ Connection
Returns a new instance of Connection.
25 26 27 28 29 30 31 32 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 25 def initialize(arguments={}) @host = arguments[:host] @connection = arguments[:connection] @options = arguments[:options] || {} @options[:resurrect_timeout] ||= DEFAULT_RESURRECT_TIMEOUT @failures = 0 end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
19 20 21 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 19 def connection @connection end |
#dead_since ⇒ Object (readonly)
Returns the value of attribute dead_since.
19 20 21 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 19 def dead_since @dead_since end |
#failures ⇒ Object (readonly)
Returns the value of attribute failures.
19 20 21 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 19 def failures @failures end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
19 20 21 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 19 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 19 def @options end |
Instance Method Details
#alive! ⇒ self
Marks this connection as alive, ie. it is eligible to be returned from the pool by the selector.
78 79 80 81 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 78 def alive! @dead = false self end |
#dead! ⇒ self
Marks this connection as dead, incrementing the ‘failures` counter and storing the current time as `dead_since`.
67 68 69 70 71 72 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 67 def dead! @dead = true @failures += 1 @dead_since = Time.now self end |
#dead? ⇒ Boolean
Returns true when this connection has been marked dead, false otherwise.
58 59 60 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 58 def dead? @dead || false end |
#full_path(path, params = {}) ⇒ String
Returns the complete endpoint path with serialized parameters.
50 51 52 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 50 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.
38 39 40 41 42 43 44 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 38 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! ⇒ self
Marks this connection as healthy, ie. a request has been successfully performed with it.
87 88 89 90 91 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 87 def healthy! @dead = false @failures = 0 self end |
#resurrect! ⇒ self?
Marks this connection as alive, if the required timeout has passed.
99 100 101 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 99 def resurrect! alive! if resurrectable? end |
#resurrectable? ⇒ Boolean
Returns true if the connection is eligible to be resurrected as alive, false otherwise.
107 108 109 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 107 def resurrectable? Time.now > @dead_since + ( @options[:resurrect_timeout] * 2 ** (@failures-1) ) end |
#to_s ⇒ String
113 114 115 |
# File 'lib/elasticsearch/transport/transport/connections/connection.rb', line 113 def to_s "<#{self.class.name} host: #{host} (#{dead? ? 'dead since ' + dead_since.to_s : 'alive'})>" end |