Skip to content

Commit c51b8fd

Browse files
committed
Refactored sniff_hosts for easier implementation of AsyncTransport
1 parent 64ff08b commit c51b8fd

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

elasticsearch/connection/http_requests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
3131
if not REQUESTS_AVAILABLE:
3232
raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.")
3333

34-
super(RequestsHttpConnection, self).__init__(host= host, port=port, **kwargs)
34+
super(RequestsHttpConnection, self).__init__(host=host, port=port, **kwargs)
3535
self.session = requests.session()
3636
if http_auth is not None:
3737
if isinstance(http_auth, (tuple, list)):
@@ -91,4 +91,4 @@ def close(self):
9191
"""
9292
Explicitly closes connections
9393
"""
94-
self.session.close()
94+
self.session.close()

elasticsearch/transport.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ def _get_sniff_data(self, initial=False):
218218

219219
return list(node_info['nodes'].values())
220220

221+
def _get_host_info(self, host_info):
222+
address_key = self.connection_class.transport_schema + '_address'
223+
host = {}
224+
address = host_info.get(address_key, '')
225+
if '/' in address:
226+
host['host'], address = address.split('/', 1)
227+
228+
# malformed address
229+
if ':' not in address:
230+
return None
231+
232+
ip, port = address.rsplit(':', 1)
233+
234+
# use the ip if not overridden by publish_host
235+
host.setdefault('host', ip)
236+
host['port'] = int(port)
237+
238+
return self.host_info_callback(host_info, host)
221239

222240
def sniff_hosts(self, initial=False):
223241
"""
@@ -231,27 +249,7 @@ def sniff_hosts(self, initial=False):
231249
"""
232250
node_info = self._get_sniff_data(initial)
233251

234-
hosts = []
235-
address_key = self.connection_class.transport_schema + '_address'
236-
for n in node_info:
237-
host = {}
238-
address = n.get(address_key, '')
239-
if '/' in address:
240-
host['host'], address = address.split('/', 1)
241-
242-
# malformed address
243-
if ':' not in address:
244-
continue
245-
246-
ip, port = address.rsplit(':', 1)
247-
248-
# use the ip if not overridden by publish_host
249-
host.setdefault('host', ip)
250-
host['port'] = int(port)
251-
252-
host = self.host_info_callback(n, host)
253-
if host is not None:
254-
hosts.append(host)
252+
hosts = list(filter(None, (self._get_host_info(n) for n in node_info)))
255253

256254
# we weren't able to get any nodes, maybe using an incompatible
257255
# transport_schema or host_info_callback blocked all - raise error.

0 commit comments

Comments
 (0)