44from .connection import Urllib3HttpConnection
55from .connection_pool import ConnectionPool
66from .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]"
1011ADDRESS_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