38
38
)
39
39
from tarantool .space import Space
40
40
from tarantool .const import (
41
+ CONNECTION_TIMEOUT ,
41
42
SOCKET_TIMEOUT ,
42
43
RECONNECT_MAX_ATTEMPTS ,
43
44
RECONNECT_DELAY ,
@@ -89,7 +90,8 @@ def __init__(self, host, port,
89
90
reconnect_delay = RECONNECT_DELAY ,
90
91
connect_now = True ,
91
92
encoding = ENCODING_DEFAULT ,
92
- call_16 = False ):
93
+ call_16 = False ,
94
+ connection_timeout = CONNECTION_TIMEOUT ):
93
95
'''
94
96
Initialize a connection to the server.
95
97
@@ -124,6 +126,7 @@ def __init__(self, host, port,
124
126
self .error = True
125
127
self .encoding = encoding
126
128
self .call_16 = call_16
129
+ self .connection_timeout = connection_timeout
127
130
if connect_now :
128
131
self .connect ()
129
132
@@ -151,7 +154,9 @@ def connect_tcp(self):
151
154
self .connected = True
152
155
if self ._socket :
153
156
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 )
155
160
except socket .error as e :
156
161
self .connected = False
157
162
raise NetworkError (e )
@@ -168,7 +173,9 @@ def connect_unix(self):
168
173
if self ._socket :
169
174
self ._socket .close ()
170
175
self ._socket = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
176
+ self ._socket .settimeout (self .connection_timeout )
171
177
self ._socket .connect (self .port )
178
+ self ._socket .settimeout (self .socket_timeout )
172
179
except socket .error as e :
173
180
self .connected = False
174
181
raise NetworkError (e )
@@ -339,11 +346,6 @@ def check(): # Check that connection is alive
339
346
except :
340
347
self .inconnect = False
341
348
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 )
347
349
348
350
def _send_request (self , request ):
349
351
'''
0 commit comments