From 7bd2b836682706cc14006375520d4e4957b4964a Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 14 Dec 2023 16:22:07 +0800 Subject: [PATCH 1/3] fix: connect_timeout param not take effect --- databend_py/connection.py | 3 +++ databend_py/defines.py | 1 + databend_py/retry.py | 3 +++ tests/test_client.py | 4 ++++ 4 files changed, 11 insertions(+) diff --git a/databend_py/connection.py b/databend_py/connection.py index 93c0b55..1a5c1fc 100644 --- a/databend_py/connection.py +++ b/databend_py/connection.py @@ -73,6 +73,7 @@ class Connection(object): # 'database': 'default' # } def __init__(self, host, port=None, user=defines.DEFAULT_USER, password=defines.DEFAULT_PASSWORD, + connect_timeout=defines.DEFAULT_CONNECT_TIMEOUT, database=defines.DEFAULT_DATABASE, secure=False, copy_purge=False, session_settings=None, persist_cookies=False): self.host = host @@ -80,6 +81,7 @@ def __init__(self, host, port=None, user=defines.DEFAULT_USER, password=defines. self.user = user self.password = password self.database = database + self.connect_timeout = connect_timeout self.secure = secure self.copy_purge = copy_purge self.session_max_idle_time = defines.DEFAULT_SESSION_IDLE_TIME @@ -124,6 +126,7 @@ def do_query(self, url, query_sql): data=json.dumps(query_sql), headers=self.make_headers(), auth=HTTPBasicAuth(self.user, self.password), + timeout=self.connect_timeout, verify=True) try: resp_dict = json.loads(response.content) diff --git a/databend_py/defines.py b/databend_py/defines.py index 936467b..e1fc2d8 100644 --- a/databend_py/defines.py +++ b/databend_py/defines.py @@ -2,6 +2,7 @@ DEFAULT_USER = 'root' DEFAULT_PASSWORD = '' DEFAULT_SESSION_IDLE_TIME = 30 +DEFAULT_CONNECT_TIMEOUT = 20 DBMS_NAME = 'Databend' CLIENT_NAME = 'databend-py' diff --git a/databend_py/retry.py b/databend_py/retry.py index 8d263fa..d41166d 100644 --- a/databend_py/retry.py +++ b/databend_py/retry.py @@ -1,3 +1,5 @@ +import time + from databend_py.errors import WarehouseTimeoutException @@ -22,6 +24,7 @@ def newfn(*args, **kwargs): 'Exception thrown when attempting to run %s, attempt ' '%d of %d' % (func, attempt, times) ) + time.sleep(attempt) attempt += 1 return func(*args, **kwargs) diff --git a/tests/test_client.py b/tests/test_client.py index 3716c56..1a9da41 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -46,6 +46,10 @@ def test_simple(self): self.assertEqual(c.connection.schema, "http") c = Client.from_url("databend://root:root@localhost:8000/default?compress=True") self.assertEqual(c._uploader._compress, True) + self.assertEqual(c.connection.connect_timeout, 20) + + c = Client.from_url("databend://root:root@localhost:8000/default?connect_timeout=30") + self.assertEqual(c.connection.connect_timeout, 30) self.assertEqual(c.connection.persist_cookies, False) c = Client.from_url('https://root:root@localhost:8000?persist_cookies=True') From 1f1ae270e943f4a4137eba1bd9a662bee98dcbf8 Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 14 Dec 2023 16:26:41 +0800 Subject: [PATCH 2/3] add docs --- docs/connection.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/docs/connection.md b/docs/connection.md index 3eb3e24..f2d1bda 100644 --- a/docs/connection.md +++ b/docs/connection.md @@ -15,20 +15,22 @@ client = Client( database="default", user="user", port="443", + secure=True, password="password", settings={"copy_purge": True, "force": True}) ``` ### Parameter References -| Parameter | Description | Default | example | -|------------------|----------------------------------------------------------------------------------------------------------|-------------|------------------------------------------------| -| user | username | root | | -| password | password | None | | | -| port | server port | None | | -| database | selected database | default | -| secure | Enable SSL | false | http://root@localhost:8000/db?secure=False | -| copy_purge | If True, the command will purge the files in the stage after they are loaded successfully into the table | false | http://root@localhost:8000/db?copy_purge=False | -| debug | Enable debug log | False | http://root@localhost:8000/db?debug=True | -| persist_cookies | if using cookies set by server to perform following requests. | False | http://root@localhost:8000/db?persist_cookies=True| -| null_to_none | if the result data NULL which is of type str, change it to NoneType | False | http://root@localhost:8000/db?null_to_none=True| +| Parameter | Description | Default | example | +|-------------------|----------------------------------------------------------------------------------------------------------|---------|-----------------------------------------------------------| +| user | username | root | | +| password | password | None | | | +| port | server port | None | | +| database | selected database | default | +| secure | Enable SSL | false | http://root@localhost:8000/db?secure=False | +| copy_purge | If True, the command will purge the files in the stage after they are loaded successfully into the table | false | http://root@localhost:8000/db?copy_purge=False | +| debug | Enable debug log | False | http://root@localhost:8000/db?debug=True | +| persist_cookies | if using cookies set by server to perform following requests. | False | http://root@localhost:8000/db?persist_cookies=True | +| null_to_none | if the result data NULL which is of type str, change it to NoneType | False | http://root@localhost:8000/db?null_to_none=True | +| connect_timeout | timeout seconds when connect to databend | 20 | http://root:root@localhost:8000/db?connect_timeout=30 | From 39b88b1c826699681f2990ed3f906f89d9da013f Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 14 Dec 2023 16:29:27 +0800 Subject: [PATCH 3/3] fix --- databend_py/retry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/databend_py/retry.py b/databend_py/retry.py index d41166d..1fb75d3 100644 --- a/databend_py/retry.py +++ b/databend_py/retry.py @@ -24,7 +24,7 @@ def newfn(*args, **kwargs): 'Exception thrown when attempting to run %s, attempt ' '%d of %d' % (func, attempt, times) ) - time.sleep(attempt) + time.sleep(attempt * 5) attempt += 1 return func(*args, **kwargs)