From a5aa160b81c551d1649baf81a24e9b0c73bd6266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Mon, 7 Dec 2015 18:26:00 +0100 Subject: [PATCH] Factored out the ES communication when sniffing This should help with #312 --- elasticsearch/transport.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index 9417bf8f..8cd90b0a 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -181,17 +181,14 @@ class Transport(object): self.sniff_hosts() return self.connection_pool.get_connection() - def sniff_hosts(self, initial=False): + def _get_sniff_data(self, initial=False): """ - Obtain a list of nodes from the cluster and create a new connection - pool using the information retrieved. - - To extract the node connection parameters use the ``nodes_to_host_callback``. - - :arg initial: flag indicating if this is during startup - (``sniff_on_start``), ignore the ``sniff_timeout`` if ``True`` + Perform the request to get sniffins information. Returns a list of + dictionaries (one per node) containing all the information from the + cluster. """ previous_sniff = self.last_sniff + try: # reset last_sniff timestamp self.last_sniff = time.time() @@ -213,9 +210,24 @@ class Transport(object): self.last_sniff = previous_sniff raise + return list(node_info['nodes'].values()) + + + def sniff_hosts(self, initial=False): + """ + Obtain a list of nodes from the cluster and create a new connection + pool using the information retrieved. + + To extract the node connection parameters use the ``nodes_to_host_callback``. + + :arg initial: flag indicating if this is during startup + (``sniff_on_start``), ignore the ``sniff_timeout`` if ``True`` + """ + node_info = self._get_sniff_data(initial) + hosts = [] address_key = self.connection_class.transport_schema + '_address' - for n in node_info['nodes'].values(): + for n in node_info: host = {} address = n.get(address_key, '') if '/' in address: