Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
)
from tarantool.space import Space
from tarantool.const import (
CONNECTION_TIMEOUT,
SOCKET_TIMEOUT,
RECONNECT_MAX_ATTEMPTS,
RECONNECT_DELAY,
Expand Down Expand Up @@ -89,7 +90,8 @@ def __init__(self, host, port,
reconnect_delay=RECONNECT_DELAY,
connect_now=True,
encoding=ENCODING_DEFAULT,
call_16=False):
call_16=False,
connection_timeout=CONNECTION_TIMEOUT):
'''
Initialize a connection to the server.

Expand Down Expand Up @@ -124,6 +126,7 @@ def __init__(self, host, port,
self.error = True
self.encoding = encoding
self.call_16 = call_16
self.connection_timeout = connection_timeout
if connect_now:
self.connect()

Expand Down Expand Up @@ -151,7 +154,9 @@ def connect_tcp(self):
self.connected = True
if self._socket:
self._socket.close()
self._socket = socket.create_connection((self.host, self.port))
self._socket = socket.create_connection(
(self.host, self.port), timeout=self.connection_timeout)
self._socket.settimeout(self.socket_timeout)
except socket.error as e:
self.connected = False
raise NetworkError(e)
Expand All @@ -168,7 +173,9 @@ def connect_unix(self):
if self._socket:
self._socket.close()
self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._socket.settimeout(self.connection_timeout)
self._socket.connect(self.port)
self._socket.settimeout(self.socket_timeout)
except socket.error as e:
self.connected = False
raise NetworkError(e)
Expand Down Expand Up @@ -339,11 +346,6 @@ def check(): # Check that connection is alive
except:
self.inconnect = False
raise
# It is important to set socket timeout *after* connection.
# Otherwise the timeout exception will be raised, even when
# the connection fails because the server is simply
# not bound to port
self._socket.settimeout(self.socket_timeout)

def _send_request(self, request):
'''
Expand Down
2 changes: 2 additions & 0 deletions tarantool/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
ITERATOR_OVERLAPS = 10
ITERATOR_NEIGHBOR = 11

# Default value for connection timeout (seconds)
CONNECTION_TIMEOUT = None
# Default value for socket timeout (seconds)
SOCKET_TIMEOUT = None
# Default maximum number of attempts to reconnect
Expand Down