lib/elasticsearch/transport/transport/sniffer.rb in elasticsearch-transport-6.8.1 vs lib/elasticsearch/transport/transport/sniffer.rb in elasticsearch-transport-6.8.2

- old
+ new

@@ -1,5 +1,9 @@ +# Licensed to Elasticsearch B.V under one or more agreements. +# Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +# See the LICENSE file in the project root for more information + module Elasticsearch module Transport module Transport # Handles node discovery ("sniffing") @@ -26,15 +30,16 @@ # @return [Array<Hash>] # @raise [SnifferTimeoutError] # def hosts Timeout::timeout(timeout, SnifferTimeoutError) do - nodes = transport.perform_request('GET', '_nodes/http').body + nodes = transport.perform_request('GET', '_nodes/http', {}, nil, nil, + reload_on_failure: false).body - hosts = nodes['nodes'].map do |id,info| + hosts = nodes['nodes'].map do |id, info| if info[PROTOCOL] - host, port = info[PROTOCOL]['publish_address'].split(':') + host, port = parse_publish_address(info[PROTOCOL]['publish_address']) { :id => id, :name => info['name'], :version => info['version'], :host => host, @@ -44,9 +49,32 @@ end end.compact hosts.shuffle! if transport.options[:randomize_hosts] hosts + end + end + + private + + 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 + + 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 end end end