Sha256: b1ee56be9c33c26c22f70ff6429d5b9e988e42c6523cca6b38348d4a4fb5ff86

Contents?: true

Size: 1.57 KB

Versions: 13

Compression:

Stored size: 1.57 KB

Contents

module Elasticsearch
  module Transport
    module Transport

      # Handles node discovery ("sniffing").
      #
      class Sniffer
        RE_URL  = /\/([^:]*):([0-9]+)\]/ # Use named groups on Ruby 1.9: /\/(?<host>[^:]*):(?<port>[0-9]+)\]/

        attr_reader   :transport
        attr_accessor :timeout

        # @param transport [Object] A transport instance.
        #
        def initialize(transport)
          @transport = transport
          @timeout   = transport.options[:sniffer_timeout] || 1
        end

        # Retrieves the node list from the Elasticsearch's
        # [_Nodes Info API_](http://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]
        #
        def hosts
          Timeout::timeout(timeout, SnifferTimeoutError) do
            nodes = transport.perform_request('GET', '_nodes/http').body
            hosts = nodes['nodes'].map do |id,info|
              if matches = info["#{transport.protocol}_address"].to_s.match(RE_URL)
                # TODO: Implement lightweight "indifferent access" here
                info.merge :host => matches[1], :port => matches[2], :id => id
              end
            end.compact

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

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
elasticsearch-transport-sinneduy-1.0.12 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-pixlee-1.0.13 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.12 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.11 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.10 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.9 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.7 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.6 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.5 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.4 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.2 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.1 lib/elasticsearch/transport/transport/sniffer.rb
elasticsearch-transport-1.0.0 lib/elasticsearch/transport/transport/sniffer.rb