Skip to content

Commit c1fd698

Browse files
Froger Davidgijzelaerr
authored andcommitted
add timeout for server connection (#22)
1 parent 385c873 commit c1fd698

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

pymonetdb/control.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Control:
7070
stop, lock, unlock, destroy your databases and request status information.
7171
"""
7272
def __init__(self, hostname=None, port=50000, passphrase=None,
73-
unix_socket=None):
73+
unix_socket=None, connect_timeout=-1):
7474

7575
if not unix_socket:
7676
unix_socket = "/tmp/.s.merovingian.%i" % port
@@ -83,19 +83,22 @@ def __init__(self, hostname=None, port=50000, passphrase=None,
8383
self.port = port
8484
self.passphrase = passphrase
8585
self.unix_socket = unix_socket
86+
self.connect_timeout = connect_timeout
8687

8788
# check connection
8889
self.server.connect(hostname=hostname, port=port, username='monetdb',
8990
password=passphrase,
9091
database='merovingian', language='control',
91-
unix_socket=unix_socket)
92+
unix_socket=unix_socket,
93+
connect_timeout=connect_timeout)
9294
self.server.disconnect()
9395

9496
def _send_command(self, database_name, command):
9597
self.server.connect(hostname=self.hostname, port=self.port,
9698
username='monetdb', password=self.passphrase,
9799
database='merovingian', language='control',
98-
unix_socket=self.unix_socket)
100+
unix_socket=self.unix_socket,
101+
connect_timeout=self.connect_timeout)
99102
try:
100103
return self.server.cmd("%s %s\n" % (database_name, command))
101104
finally:

pymonetdb/mapi.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ def __init__(self):
100100
self.password = ""
101101
self.database = ""
102102
self.language = ""
103+
self.connect_timeout = socket.getdefaulttimeout()
103104

104105
def connect(self, database, username, password, language, hostname=None,
105-
port=None, unix_socket=None):
106+
port=None, unix_socket=None, connect_timeout=-1):
106107
""" setup connection to MAPI server
107108
108109
unix_socket is used if hostname is not defined.
@@ -116,6 +117,11 @@ def connect(self, database, username, password, language, hostname=None,
116117
elif not hostname:
117118
hostname = 'localhost'
118119

120+
# None and zero are allowed values
121+
if connect_timeout != -1:
122+
assert connect_timeout is None or connect_timeout >= 0
123+
self.connect_timeout = connect_timeout
124+
119125
self.hostname = hostname
120126
self.port = port
121127
self.username = username
@@ -129,9 +135,11 @@ def connect(self, database, username, password, language, hostname=None,
129135
# For performance, mirror MonetDB/src/common/stream.c socket settings.
130136
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 0)
131137
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
138+
self.socket.settimeout(self.connect_timeout)
132139
self.socket.connect((hostname, port))
133140
else:
134141
self.socket = socket.socket(socket.AF_UNIX)
142+
self.socket.settimeout(self.connect_timeout)
135143
self.socket.connect(unix_socket)
136144
if self.language != 'control':
137145
# don't know why, but we need to do this
@@ -141,6 +149,7 @@ def connect(self, database, username, password, language, hostname=None,
141149
# control doesn't require authentication over socket
142150
self._login()
143151

152+
self.socket.settimeout(socket.getdefaulttimeout())
144153
self.state = STATE_READY
145154

146155
def _login(self, iteration=0):

pymonetdb/sql/connections.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Connection(object):
2020

2121
def __init__(self, database, hostname=None, port=50000, username="monetdb",
2222
password="monetdb", unix_socket=None, autocommit=False,
23-
host=None, user=None):
23+
host=None, user=None, connect_timeout=-1):
2424
""" Set up a connection to a MonetDB SQL database.
2525
2626
args:
@@ -32,6 +32,8 @@ def __init__(self, database, hostname=None, port=50000, username="monetdb",
3232
unix_socket (str): socket to connect to. used when hostname not set
3333
(default: "/tmp/.s.monetdb.50000")
3434
autocommit (bool): enable/disable auto commit (default: False)
35+
connect_timeout -- the socket timeout while connecting
36+
(default: see python socket module)
3537
3638
returns:
3739
Connection object
@@ -50,7 +52,7 @@ def __init__(self, database, hostname=None, port=50000, username="monetdb",
5052
self.mapi = mapi.Connection()
5153
self.mapi.connect(hostname=hostname, port=int(port), username=username,
5254
password=password, database=database, language="sql",
53-
unix_socket=unix_socket)
55+
unix_socket=unix_socket, connect_timeout=connect_timeout)
5456
self.set_autocommit(autocommit)
5557
self.set_sizeheader(True)
5658
self.set_replysize(100)

0 commit comments

Comments
 (0)