Skip to content

Commit b17d96e

Browse files
committed
Add connection_timeout parameter
Fixes #115.
1 parent 14f52b5 commit b17d96e

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

tarantool/connection.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
)
3939
from tarantool.space import Space
4040
from 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
'''

tarantool/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
ITERATOR_OVERLAPS = 10
7979
ITERATOR_NEIGHBOR = 11
8080

81+
# Default value for connection timeout (seconds)
82+
CONNECTION_TIMEOUT = None
8183
# Default value for socket timeout (seconds)
8284
SOCKET_TIMEOUT = None
8385
# Default maximum number of attempts to reconnect

0 commit comments

Comments
 (0)