Skip to content

Commit d011673

Browse files
committed
SQLAlchemy: Fix handling URL parameters timeout and pool_size
1 parent d2d44a5 commit d011673

4 files changed

Lines changed: 59 additions & 5 deletions

File tree

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Unreleased
66
==========
77

88
- Properly handle Python-native UUID types in SQL parameters
9+
- SQLAlchemy: Fix handling URL parameters ``timeout`` and ``pool_size``
10+
911

1012
2023/07/17 0.33.0
1113
=================

docs/by-example/sqlalchemy/getting-started.rst

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Create an SQLAlchemy :doc:`Session <sa:orm/session_basics>`:
4646
>>> Base = declarative_base()
4747

4848

49-
Connection string
50-
=================
49+
Connect
50+
=======
5151

5252
In SQLAlchemy, a connection is established using the ``create_engine`` function.
5353
This function takes a connection string, actually an `URL`_, that varies from
@@ -65,7 +65,9 @@ to a different server the following syntax can be used:
6565
>>> sa.create_engine('crate://otherserver:4200')
6666
Engine(crate://otherserver:4200)
6767

68-
Since CrateDB is a clustered database running on multiple servers, it is
68+
Multiple Hosts
69+
--------------
70+
Because CrateDB is a clustered database running on multiple servers, it is
6971
recommended to connect to all of them. This enables the DB-API layer to
7072
use round-robin to distribute the load and skip a server if it becomes
7173
unavailable. In order to make the driver aware of multiple servers, use
@@ -76,6 +78,8 @@ the ``connect_args`` parameter like so:
7678
... })
7779
Engine(crate://)
7880

81+
TLS Options
82+
-----------
7983
As defined in :ref:`https_connection`, the client validates SSL server
8084
certificates by default. To configure this further, use e.g. the ``ca_cert``
8185
attribute within the ``connect_args``, like:
@@ -96,6 +100,37 @@ In order to disable SSL verification, use ``verify_ssl_cert = False``, like:
96100
... 'verify_ssl_cert': False,
97101
... })
98102

103+
Timeout Options
104+
---------------
105+
In order to configure TCP timeout options, use the ``timeout`` parameter within
106+
``connect_args``,
107+
108+
>>> timeout_engine = sa.create_engine('crate://localhost/', connect_args={'timeout': 42.42})
109+
>>> timeout_engine.raw_connection().driver_connection.client._pool_kw["timeout"]
110+
42.42
111+
112+
or use the ``timeout`` URL parameter within the database connection URL.
113+
114+
>>> timeout_engine = sa.create_engine('crate://localhost/?timeout=42.42')
115+
>>> timeout_engine.raw_connection().driver_connection.client._pool_kw["timeout"]
116+
42.42
117+
118+
Pool Size
119+
---------
120+
121+
In order to configure the database connection pool size, use the ``pool_size``
122+
parameter within ``connect_args``,
123+
124+
>>> timeout_engine = sa.create_engine('crate://localhost/', connect_args={'pool_size': 20})
125+
>>> timeout_engine.raw_connection().driver_connection.client._pool_kw["maxsize"]
126+
20
127+
128+
or use the ``pool_size`` URL parameter within the database connection URL.
129+
130+
>>> timeout_engine = sa.create_engine('crate://localhost/?pool_size=20')
131+
>>> timeout_engine.raw_connection().driver_connection.client._pool_kw["maxsize"]
132+
20
133+
99134

100135
Basic DDL operations
101136
====================

src/crate/client/http.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,11 @@ def _pool_kw_args(verify_ssl_cert, ca_cert, client_cert, client_key,
255255
'cert_reqs': ssl.CERT_REQUIRED if verify_ssl_cert else ssl.CERT_NONE,
256256
'cert_file': client_cert,
257257
'key_file': client_key,
258-
'timeout': timeout,
259258
}
259+
if timeout is not None:
260+
kw['timeout'] = float(timeout)
260261
if pool_size is not None:
261-
kw['maxsize'] = pool_size
262+
kw['maxsize'] = int(pool_size)
262263
return kw
263264

264265

src/crate/client/sqlalchemy/tests/connection_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ def test_connection_server_uri_https_with_credentials(self):
8383
conn.close()
8484
engine.dispose()
8585

86+
def test_connection_server_uri_parameter_timeout(self):
87+
engine = sa.create_engine(
88+
"crate://otherhost:19201/?timeout=42.42")
89+
conn = engine.raw_connection()
90+
self.assertEqual(conn.driver_connection.client._pool_kw["timeout"], 42.42)
91+
conn.close()
92+
engine.dispose()
93+
94+
def test_connection_server_uri_parameter_pool_size(self):
95+
engine = sa.create_engine(
96+
"crate://otherhost:19201/?pool_size=20")
97+
conn = engine.raw_connection()
98+
self.assertEqual(conn.driver_connection.client._pool_kw["maxsize"], 20)
99+
conn.close()
100+
engine.dispose()
101+
86102
def test_connection_multiple_server_http(self):
87103
engine = sa.create_engine(
88104
"crate://", connect_args={

0 commit comments

Comments
 (0)