Skip to content

Commit 7171797

Browse files
committed
Add connect_timeout parameter
1 parent e72a373 commit 7171797

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

singlestoredb/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@
181181
environ='SINGLESTOREDB_BUFFERED',
182182
)
183183

184+
register_option(
185+
'connect_timeout', 'int', check_int, 10,
186+
'The timeout for connecting to the database in seconds. '
187+
'(default: 10, min: 1, max: 31536000)',
188+
environ='SINGLESTOREDB_CONNECT_TIMEOUT',
189+
)
190+
184191
#
185192
# Query results options
186193
#

singlestoredb/connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ def connect(
12771277
program_name: Optional[str] = None,
12781278
conn_attrs: Optional[Dict[str, str]] = None,
12791279
multi_statements: Optional[bool] = None,
1280+
connect_timeout: Optional[int] = None,
12801281
) -> Connection:
12811282
"""
12821283
Return a SingleStoreDB connection.
@@ -1338,6 +1339,11 @@ def connect(
13381339
conn_attrs: dict, optional
13391340
Additional connection attributes for telemetry. Example:
13401341
{'program_version': "1.0.2", "_connector_name": "dbt connector"}
1342+
multi_statements: bool, optional
1343+
Should multiple statements be allowed within a single query?
1344+
connect_timeout: int, optional
1345+
The timeout for connecting to the database in seconds.
1346+
(default: 10, min: 1, max: 31536000)
13411347
13421348
Examples
13431349
--------

singlestoredb/http/connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ def __init__(self, **kwargs: Any):
809809
if not ssl_verify_cert:
810810
self._sess.verify = False
811811

812+
if kwargs.get('multi_statements', False):
813+
raise self.InterfaceError(
814+
0, 'The Data API does not allow multiple '
815+
'statements within a query',
816+
)
817+
812818
version = kwargs.get('version', 'v2')
813819
self.driver = kwargs.get('driver', 'https')
814820

@@ -846,6 +852,8 @@ def _post(self, path: str, *args: Any, **kwargs: Any) -> requests.Response:
846852
"""
847853
if self._sess is None:
848854
raise InterfaceError(errno=2048, msg='Connection is closed.')
855+
if 'timeout' not in kwargs:
856+
kwargs['timeout'] = get_option('connect_timeout')
849857
return self._sess.post(urljoin(self._url, path), *args, **kwargs)
850858

851859
def close(self) -> None:

singlestoredb/tests/test_connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,12 @@ def test_multi_statements(self):
13701370
self.assertEqual([(2,)], list(cur))
13711371
self.assertIsNone(cur.nextset())
13721372

1373+
def test_connect_timeout(self):
1374+
with s2.connect(database=type(self).dbname, connect_timeout=8) as conn:
1375+
with conn.cursor() as cur:
1376+
cur.execute('SELECT 1')
1377+
self.assertEqual([(1,)], list(cur))
1378+
13731379
def test_show_accessors(self):
13741380
out = self.conn.show.columns('data')
13751381
assert out.columns == [

0 commit comments

Comments
 (0)