Make initial sniff (sniff_on_start) ignore sniff_timeout

This is due to issue mentioned in #167 as part of discussion
This commit is contained in:
Honza Král
2015-01-04 19:44:21 +01:00
parent b3c2ef2996
commit d3e745ab77
2 changed files with 20 additions and 5 deletions
+10 -5
View File
@@ -61,7 +61,9 @@ class Transport(object):
:arg sniff_on_connection_fail: flag controlling if connection failure triggers a sniff :arg sniff_on_connection_fail: flag controlling if connection failure triggers a sniff
:arg sniff_timeout: timeout used for the sniff request - it should be a :arg sniff_timeout: timeout used for the sniff request - it should be a
fast api call and we are talking potentially to more nodes so we want fast api call and we are talking potentially to more nodes so we want
to fail quickly. to fail quickly. Not used during initial sniffing (if
``sniff_on_start`` is on) when the connection still isn't
initialized.
:arg serializer: serializer instance :arg serializer: serializer instance
:arg serializers: optional dict of serializer instances that will be :arg serializers: optional dict of serializer instances that will be
used for deserializing data coming from the server. (key is the mimetype) used for deserializing data coming from the server. (key is the mimetype)
@@ -124,7 +126,7 @@ class Transport(object):
self.host_info_callback = host_info_callback self.host_info_callback = host_info_callback
if sniff_on_start: if sniff_on_start:
self.sniff_hosts() self.sniff_hosts(True)
def add_connection(self, host): def add_connection(self, host):
""" """
@@ -182,12 +184,15 @@ class Transport(object):
self.sniff_hosts() self.sniff_hosts()
return self.connection_pool.get_connection() return self.connection_pool.get_connection()
def sniff_hosts(self): def sniff_hosts(self, initial=False):
""" """
Obtain a list of nodes from the cluster and create a new connection Obtain a list of nodes from the cluster and create a new connection
pool using the information retrieved. pool using the information retrieved.
To extract the node connection parameters use the `nodes_to_host_callback`. 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``
""" """
previous_sniff = self.last_sniff previous_sniff = self.last_sniff
try: try:
@@ -199,7 +204,7 @@ class Transport(object):
try: try:
# use small timeout for the sniffing request, should be a fast api call # use small timeout for the sniffing request, should be a fast api call
_, headers, node_info = c.perform_request('GET', '/_nodes/_all/clear', _, headers, node_info = c.perform_request('GET', '/_nodes/_all/clear',
timeout=self.sniff_timeout) timeout=self.sniff_timeout if not initial else None)
node_info = self.deserializer.loads(node_info, headers.get('content-type')) node_info = self.deserializer.loads(node_info, headers.get('content-type'))
break break
except (ConnectionError, SerializationError): except (ConnectionError, SerializationError):
+10
View File
@@ -168,6 +168,16 @@ class TestTransport(TestCase):
self.assertEquals(1, len(t.connection_pool.connections)) self.assertEquals(1, len(t.connection_pool.connections))
self.assertEquals('http://1.1.1.1:123', t.get_connection().host) self.assertEquals('http://1.1.1.1:123', t.get_connection().host)
def test_sniff_on_start_ignores_sniff_timeout(self):
t = Transport([{'data': CLUSTER_NODES}], connection_class=DummyConnection, sniff_on_start=True, sniff_timeout=12)
self.assertEquals((('GET', '/_nodes/_all/clear'), {'timeout': None}), t.seed_connections[0].calls[0])
def test_sniff_uses_sniff_timeout(self):
t = Transport([{'data': CLUSTER_NODES}], connection_class=DummyConnection, sniff_timeout=42)
t.sniff_hosts()
self.assertEquals((('GET', '/_nodes/_all/clear'), {'timeout': 42}), t.seed_connections[0].calls[0])
def test_sniff_reuses_connection_instances_if_possible(self): def test_sniff_reuses_connection_instances_if_possible(self):
t = Transport([{'data': CLUSTER_NODES}, {"host": "1.1.1.1", "port": 123}], connection_class=DummyConnection, randomize_hosts=False) t = Transport([{'data': CLUSTER_NODES}, {"host": "1.1.1.1", "port": 123}], connection_class=DummyConnection, randomize_hosts=False)
connection = t.connection_pool.connections[1] connection = t.connection_pool.connections[1]