Class: OpenSearch::Transport::Transport::Sniffer

Inherits:
Object
  • Object
show all
Defined in:
lib/opensearch/transport/transport/sniffer.rb

Overview

Handles node discovery (“sniffing”)

Constant Summary collapse

PROTOCOL =
'http'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Sniffer

Returns a new instance of Sniffer.

Parameters:

  • transport (Object)

    A transport instance



41
42
43
44
# File 'lib/opensearch/transport/transport/sniffer.rb', line 41

def initialize(transport)
  @transport = transport
  @timeout   = transport.options[:sniffer_timeout] || 1
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



37
38
39
# File 'lib/opensearch/transport/transport/sniffer.rb', line 37

def timeout
  @timeout
end

#transportObject (readonly)

Returns the value of attribute transport.



36
37
38
# File 'lib/opensearch/transport/transport/sniffer.rb', line 36

def transport
  @transport
end

Instance Method Details

#hostsArray<Hash>

Retrieves the node list from the OpenSearch’s _Nodes Info API_ 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.

Returns:

  • (Array<Hash>)

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/opensearch/transport/transport/sniffer.rb', line 55

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