Skip to content

Commit d9296e8

Browse files
committed
Timeout should not trigger a retry by default
1 parent 2b107ec commit d9296e8

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Changelog
44
=========
55

6+
1.3.0 (dev)
7+
-----------
8+
9+
* Timeout now doesn't trigger a retry by default (can be overriden by setting
10+
`retry_on_timeout=True`)
11+
612
1.2.0 (2014-08-03)
713
------------------
814

elasticsearch/connection/http_urllib3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from urllib3.exceptions import ReadTimeoutError
44

55
from .base import Connection
6-
from ..exceptions import ConnectionError
6+
from ..exceptions import ConnectionError, ConnectionTimeout
77
from ..compat import urlencode
88

99
class Urllib3HttpConnection(Connection):
@@ -33,7 +33,7 @@ def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, m
3333
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
3434
url = self.url_prefix + url
3535
if params:
36-
url = '%s?%s' % (url, urlencode(params or {}))
36+
url = '%s?%s' % (url, urlencode(params))
3737
full_url = self.host + url
3838

3939
start = time.time()
@@ -52,7 +52,7 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign
5252
raw_data = response.data.decode('utf-8')
5353
except ReadTimeoutError as e:
5454
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
55-
raise ConnectionError('TIMEOUT', str(e), e)
55+
raise ConnectionTimeout('TIMEOUT', str(e), e)
5656
except Exception as e:
5757
self.log_request_fail(method, full_url, body, time.time() - start, exception=e)
5858
raise ConnectionError('N/A', str(e), e)

elasticsearch/transport.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from .connection import Urllib3HttpConnection
55
from .connection_pool import ConnectionPool
66
from .serializer import JSONSerializer, Deserializer, DEFAULT_SERIALIZERS
7-
from .exceptions import ConnectionError, TransportError, SerializationError
7+
from .exceptions import ConnectionError, TransportError, SerializationError, \
8+
ConnectionTimeout
89

910
# get ip/port from "inet[wind/127.0.0.1:9200]"
1011
ADDRESS_RE = re.compile(r'/(?P<host>[\.:0-9a-f]*):(?P<port>[0-9]+)\]?$')
@@ -45,7 +46,7 @@ def __init__(self, hosts, connection_class=Urllib3HttpConnection,
4546
sniff_on_start=False, sniffer_timeout=None, sniff_timeout=.1,
4647
sniff_on_connection_fail=False, serializer=JSONSerializer(), serializers=None,
4748
default_mimetype='application/json', max_retries=3,
48-
send_get_body_as='GET', **kwargs):
49+
retry_on_timeout=False, send_get_body_as='GET', **kwargs):
4950
"""
5051
:arg hosts: list of dictionaries, each containing keyword arguments to
5152
create a `connection_class` instance
@@ -67,6 +68,8 @@ def __init__(self, hosts, connection_class=Urllib3HttpConnection,
6768
:arg default_mimetype: when no mimetype is specified by the server
6869
response assume this mimetype, defaults to `'application/json'`
6970
:arg max_retries: maximum number of retries before an exception is propagated
71+
:arg retry_on_timeout: should timeout trigger a retry on different
72+
node? (default `False`)
7073
:arg send_get_body_as: for GET requests with body this option allows
7174
you to specify an alternate way of execution for environments that
7275
don't support passing bodies with GET requests. If you set this to
@@ -89,6 +92,7 @@ def __init__(self, hosts, connection_class=Urllib3HttpConnection,
8992
self.deserializer = Deserializer(_serializers, default_mimetype)
9093

9194
self.max_retries = max_retries
95+
self.retry_on_timeout = retry_on_timeout
9296
self.send_get_body_as = send_get_body_as
9397

9498
# data serializer
@@ -282,6 +286,16 @@ def perform_request(self, method, url, params=None, body=None):
282286

283287
try:
284288
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
289+
except ConnectionTimeout:
290+
if self.retry_on_timeout:
291+
# only mark as dead if we are retrying
292+
self.mark_dead(connection)
293+
# raise exception on last retry
294+
if attempt == self.max_retries:
295+
raise
296+
else:
297+
raise
298+
285299
except ConnectionError:
286300
self.mark_dead(connection)
287301

0 commit comments

Comments
 (0)