Skip to content

Commit 1f07a6a

Browse files
committed
Stopped decoding the timeout parameter strictly as scalar
Instead, also permit `urllib3.Timeout` instances.
1 parent 0fe5412 commit 1f07a6a

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Changes for crate
55
Unreleased
66
==========
77

8+
- Stopped decoding the ``timeout`` parameter strictly as scalar to also
9+
permit ``urllib3.Timeout`` instances
10+
811

912
2023/09/29 0.34.0
1013
=================

docs/by-example/client.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,25 @@ traceback if a server error occurs:
4848
>>> connection = client.connect([crate_host], error_trace=True)
4949
>>> connection.close()
5050

51+
Network Timeouts
52+
----------------
53+
5154
It's possible to define a default timeout value in seconds for all servers
52-
using the optional parameter ``timeout``:
55+
using the optional parameter ``timeout``. In this case, it will serve as a
56+
total timeout (connect and read):
5357

5458
>>> connection = client.connect([crate_host, invalid_host], timeout=5)
5559
>>> connection.close()
5660

61+
If you want to adjust the connect- vs. read-timeout values individually,
62+
please use the ``urllib3.Timeout`` object like:
63+
64+
>>> import urllib3
65+
>>> connection = client.connect(
66+
... [crate_host, invalid_host],
67+
... timeout=urllib3.Timeout(connect=5, read=None))
68+
>>> connection.close()
69+
5770
Authentication
5871
--------------
5972

docs/by-example/http.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ timeout exception:
199199
{...}
200200
>>> http_client.close()
201201

202-
It's possible to define a HTTP timeout in seconds on client instantiation, so
203-
an exception is raised when the timeout is reached:
202+
It is possible to define a HTTP timeout in seconds when creating a client
203+
object, so an exception is raised when the timeout expired:
204204

205205
>>> http_client = HttpClient(crate_host, timeout=0.01)
206206
>>> http_client.sql('select fib(32)')
@@ -209,6 +209,17 @@ an exception is raised when the timeout is reached:
209209
crate.client.exceptions.ConnectionError: No more Servers available, exception from last server: ...
210210
>>> http_client.close()
211211

212+
In order to adjust the connect- vs. read-timeout values individually,
213+
please use the ``urllib3.Timeout`` object like:
214+
215+
>>> import urllib3
216+
>>> http_client = HttpClient(crate_host, timeout=urllib3.Timeout(connect=0.01, read=None))
217+
>>> http_client.sql('select fib(32)')
218+
Traceback (most recent call last):
219+
...
220+
crate.client.exceptions.ConnectionError: No more Servers available, exception from last server: ...
221+
>>> http_client.close()
222+
212223
When connecting to non-CrateDB servers, the HttpClient will raise a ConnectionError like this:
213224

214225
>>> http_client = HttpClient(["https://example.org/"])

src/crate/client/http.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ def _pool_kw_args(verify_ssl_cert, ca_cert, client_cert, client_key,
273273
'key_file': client_key,
274274
}
275275
if timeout is not None:
276-
kw['timeout'] = float(timeout)
276+
if isinstance(timeout, str):
277+
timeout = float(timeout)
278+
kw['timeout'] = timeout
277279
if pool_size is not None:
278280
kw['maxsize'] = int(pool_size)
279281
return kw

src/crate/client/test_connection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import datetime
22

3+
from urllib3 import Timeout
4+
35
from .connection import Connection
46
from .http import Client
57
from crate.client import connect
@@ -72,3 +74,25 @@ def test_with_timezone(self):
7274
cursor = connection.cursor()
7375
self.assertEqual(cursor.time_zone.tzname(None), "UTC")
7476
self.assertEqual(cursor.time_zone.utcoffset(None), datetime.timedelta(0))
77+
78+
def test_timeout_float(self):
79+
"""
80+
Verify setting the timeout value as a scalar (float) works.
81+
"""
82+
with connect('localhost:4200', timeout=2.42) as conn:
83+
self.assertEqual(conn.client._pool_kw["timeout"], 2.42)
84+
85+
def test_timeout_string(self):
86+
"""
87+
Verify setting the timeout value as a scalar (string) works.
88+
"""
89+
with connect('localhost:4200', timeout="2.42") as conn:
90+
self.assertEqual(conn.client._pool_kw["timeout"], 2.42)
91+
92+
def test_timeout_object(self):
93+
"""
94+
Verify setting the timeout value as a Timeout object works.
95+
"""
96+
timeout = Timeout(connect=2.42, read=999)
97+
with connect('localhost:4200', timeout=timeout) as conn:
98+
self.assertEqual(conn.client._pool_kw["timeout"], timeout)

0 commit comments

Comments
 (0)