class Elasticsearch::Transport::Transport::Sniffer
Handles node discovery (“sniffing”)
Constants
- PROTOCOL
Attributes
timeout[RW]
transport[R]
Public Class Methods
new(transport)
click to toggle source
@param transport [Object] A transport instance
# File lib/elasticsearch/transport/transport/sniffer.rb, line 32 def initialize(transport) @transport = transport @timeout = transport.options[:sniffer_timeout] || 1 end
Public Instance Methods
hosts()
click to toggle source
Retrieves the node list from the Elasticsearch’s [_Nodes Info API_](www.elastic.co/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.
@return [Array<Hash>] @raise [SnifferTimeoutError]
# File lib/elasticsearch/transport/transport/sniffer.rb, line 46 def hosts Timeout::timeout(timeout, SnifferTimeoutError) do nodes = perform_sniff_request.body hosts = nodes['nodes'].map do |id, info| next unless info[PROTOCOL] host, port = parse_publish_address(info[PROTOCOL]['publish_address']) { id: id, name: info['name'], version: info['version'], host: host, port: port, roles: info['roles'], attributes: info['attributes'] } end.compact hosts.shuffle! if transport.options[:randomize_hosts] hosts end end
Private Instance Methods
parse_address_port(publish_address)
click to toggle source
# File lib/elasticsearch/transport/transport/sniffer.rb, line 89 def parse_address_port(publish_address) # address is ipv6 if publish_address =~ /[\[\]]/ if parts = publish_address.match(/\A\[(.+)\](?::(\d+))?\z/) [ parts[1], parts[2] ] end else publish_address.split(':') end end
parse_publish_address(publish_address)
click to toggle source
# File lib/elasticsearch/transport/transport/sniffer.rb, line 79 def parse_publish_address(publish_address) # publish_address is in the format hostname/ip:port if publish_address =~ /\// parts = publish_address.partition('/') [ parts[0], parse_address_port(parts[2])[1] ] else parse_address_port(publish_address) end end
perform_sniff_request()
click to toggle source
# File lib/elasticsearch/transport/transport/sniffer.rb, line 72 def perform_sniff_request transport.perform_request( 'GET', '_nodes/http', {}, nil, nil, reload_on_failure: false ) end