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

@return [Array<Hash>] @raise [SnifferTimeoutError]

# File lib/elasticsearch/transport/transport/sniffer.rb, line 29
def hosts
  Timeout::timeout(timeout, SnifferTimeoutError) do
    nodes = transport.perform_request('GET', '_nodes/http').body

    hosts = nodes['nodes'].map do |id,info|
      if info[PROTOCOL]
        host, port = info[PROTOCOL]['publish_address'].split(':')

        { :id =>      id,
          :name =>    info['name'],
          :version => info['version'],
          :host =>    host,
          :port =>    port,
          :roles =>   info['roles'],
          :attributes => info['attributes'] }
      end
    end.compact

    hosts.shuffle! if transport.options[:randomize_hosts]
    hosts
  end
end