Class: Elasticsearch::Transport::Transport::Sniffer
- Inherits:
-
Object
- Object
- Elasticsearch::Transport::Transport::Sniffer
- Defined in:
- lib/elasticsearch/transport/transport/sniffer.rb
Overview
Handles node discovery (“sniffing”)
Constant Summary collapse
- ES1_RE_URL =
/\[([^\/]*)?\/?([^:]*):([0-9]+)\]/
- ES2_RE_URL =
/([^\/]*)?\/?([^:]*):([0-9]+)/
Instance Attribute Summary collapse
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
-
#hosts ⇒ Array<Hash>
Retrieves the node list from the Elasticsearch’s [_Nodes Info API_](www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-info/) and returns a normalized Array of information suitable for passing to transport.
-
#initialize(transport) ⇒ Sniffer
constructor
A new instance of Sniffer.
Constructor Details
#initialize(transport) ⇒ Sniffer
Returns a new instance of Sniffer.
16 17 18 19 |
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 16 def initialize(transport) @transport = transport @timeout = transport.[:sniffer_timeout] || 1 end |
Instance Attribute Details
#timeout ⇒ Object
Returns the value of attribute timeout.
12 13 14 |
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 12 def timeout @timeout end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
11 12 13 |
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 11 def transport @transport end |
Instance Method Details
#hosts ⇒ Array<Hash>
Retrieves the node list from the Elasticsearch’s [_Nodes Info API_](www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-info/) and returns a normalized Array of information suitable for passing to transport.
Shuffles the collection before returning it when the ‘randomize_hosts` option is set for transport.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/elasticsearch/transport/transport/sniffer.rb', line 30 def hosts Timeout::timeout(timeout, SnifferTimeoutError) do nodes = transport.perform_request('GET', '_nodes/http').body hosts = nodes['nodes'].map do |id,info| addr_str = info["#{transport.protocol}_address"].to_s matches = addr_str.match(ES1_RE_URL) || addr_str.match(ES2_RE_URL) if matches host = matches[1].empty? ? matches[2] : matches[1] port = matches[3] info.merge :host => host, :port => port, :id => id end end.compact hosts.shuffle! if transport.[:randomize_hosts] hosts end end |