@@ -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 ):
0 commit comments