3838)
3939from tarantool .space import Space
4040from tarantool .const import (
41+ CONNECTION_TIMEOUT ,
4142 SOCKET_TIMEOUT ,
4243 RECONNECT_MAX_ATTEMPTS ,
4344 RECONNECT_DELAY ,
@@ -89,7 +90,8 @@ def __init__(self, host, port,
8990 reconnect_delay = RECONNECT_DELAY ,
9091 connect_now = True ,
9192 encoding = ENCODING_DEFAULT ,
92- call_16 = False ):
93+ call_16 = False ,
94+ connection_timeout = CONNECTION_TIMEOUT ):
9395 '''
9496 Initialize a connection to the server.
9597
@@ -124,6 +126,7 @@ def __init__(self, host, port,
124126 self .error = True
125127 self .encoding = encoding
126128 self .call_16 = call_16
129+ self .connection_timeout = connection_timeout
127130 if connect_now :
128131 self .connect ()
129132
@@ -151,7 +154,9 @@ def connect_tcp(self):
151154 self .connected = True
152155 if self ._socket :
153156 self ._socket .close ()
154- self ._socket = socket .create_connection ((self .host , self .port ))
157+ self ._socket = socket .create_connection (
158+ (self .host , self .port ), timeout = self .connection_timeout )
159+ self ._socket .settimeout (self .socket_timeout )
155160 except socket .error as e :
156161 self .connected = False
157162 raise NetworkError (e )
@@ -168,7 +173,9 @@ def connect_unix(self):
168173 if self ._socket :
169174 self ._socket .close ()
170175 self ._socket = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
176+ self ._socket .settimeout (self .connection_timeout )
171177 self ._socket .connect (self .port )
178+ self ._socket .settimeout (self .socket_timeout )
172179 except socket .error as e :
173180 self .connected = False
174181 raise NetworkError (e )
@@ -339,11 +346,6 @@ def check(): # Check that connection is alive
339346 except :
340347 self .inconnect = False
341348 raise
342- # It is important to set socket timeout *after* connection.
343- # Otherwise the timeout exception will be raised, even when
344- # the connection fails because the server is simply
345- # not bound to port
346- self ._socket .settimeout (self .socket_timeout )
347349
348350 def _send_request (self , request ):
349351 '''
0 commit comments