Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 55bdca2

Browse files
authored
Merge pull request #52 from datafuselabs/fix/connect-timeout
fix: connect_timeout param not take effect
2 parents 9d17607 + 39b88b1 commit 55bdca2

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

databend_py/connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ class Connection(object):
7373
# 'database': 'default'
7474
# }
7575
def __init__(self, host, port=None, user=defines.DEFAULT_USER, password=defines.DEFAULT_PASSWORD,
76+
connect_timeout=defines.DEFAULT_CONNECT_TIMEOUT,
7677
database=defines.DEFAULT_DATABASE, secure=False, copy_purge=False, session_settings=None,
7778
persist_cookies=False):
7879
self.host = host
7980
self.port = port
8081
self.user = user
8182
self.password = password
8283
self.database = database
84+
self.connect_timeout = connect_timeout
8385
self.secure = secure
8486
self.copy_purge = copy_purge
8587
self.session_max_idle_time = defines.DEFAULT_SESSION_IDLE_TIME
@@ -124,6 +126,7 @@ def do_query(self, url, query_sql):
124126
data=json.dumps(query_sql),
125127
headers=self.make_headers(),
126128
auth=HTTPBasicAuth(self.user, self.password),
129+
timeout=self.connect_timeout,
127130
verify=True)
128131
try:
129132
resp_dict = json.loads(response.content)

databend_py/defines.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
DEFAULT_USER = 'root'
33
DEFAULT_PASSWORD = ''
44
DEFAULT_SESSION_IDLE_TIME = 30
5+
DEFAULT_CONNECT_TIMEOUT = 20
56

67
DBMS_NAME = 'Databend'
78
CLIENT_NAME = 'databend-py'

databend_py/retry.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import time
2+
13
from databend_py.errors import WarehouseTimeoutException
24

35

@@ -22,6 +24,7 @@ def newfn(*args, **kwargs):
2224
'Exception thrown when attempting to run %s, attempt '
2325
'%d of %d' % (func, attempt, times)
2426
)
27+
time.sleep(attempt * 5)
2528
attempt += 1
2629
return func(*args, **kwargs)
2730

docs/connection.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ client = Client(
1515
database="default",
1616
user="user",
1717
port="443",
18+
secure=True,
1819
password="password", settings={"copy_purge": True, "force": True})
1920
```
2021

2122
### Parameter References
2223

23-
| Parameter | Description | Default | example |
24-
|------------------|----------------------------------------------------------------------------------------------------------|-------------|------------------------------------------------|
25-
| user | username | root | |
26-
| password | password | None | | |
27-
| port | server port | None | |
28-
| database | selected database | default |
29-
| secure | Enable SSL | false | http://root@localhost:8000/db?secure=False |
30-
| 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 |
31-
| debug | Enable debug log | False | http://root@localhost:8000/db?debug=True |
32-
| persist_cookies | if using cookies set by server to perform following requests. | False | http://root@localhost:8000/db?persist_cookies=True|
33-
| 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|
24+
| Parameter | Description | Default | example |
25+
|-------------------|----------------------------------------------------------------------------------------------------------|---------|-----------------------------------------------------------|
26+
| user | username | root | |
27+
| password | password | None | | |
28+
| port | server port | None | |
29+
| database | selected database | default |
30+
| secure | Enable SSL | false | http://root@localhost:8000/db?secure=False |
31+
| 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 |
32+
| debug | Enable debug log | False | http://root@localhost:8000/db?debug=True |
33+
| persist_cookies | if using cookies set by server to perform following requests. | False | http://root@localhost:8000/db?persist_cookies=True |
34+
| 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 |
35+
| connect_timeout | timeout seconds when connect to databend | 20 | http://root:root@localhost:8000/db?connect_timeout=30 |
3436

tests/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def test_simple(self):
4646
self.assertEqual(c.connection.schema, "http")
4747
c = Client.from_url("databend://root:root@localhost:8000/default?compress=True")
4848
self.assertEqual(c._uploader._compress, True)
49+
self.assertEqual(c.connection.connect_timeout, 20)
50+
51+
c = Client.from_url("databend://root:root@localhost:8000/default?connect_timeout=30")
52+
self.assertEqual(c.connection.connect_timeout, 30)
4953

5054
self.assertEqual(c.connection.persist_cookies, False)
5155
c = Client.from_url('https://root:root@localhost:8000?persist_cookies=True')

0 commit comments

Comments
 (0)