Skip to content

Commit 7b07121

Browse files
ValueError on invalid connection_acquisition_timeout (#1168)
- `ValueError` on invalid values (instead of `ClientError`) - Consistently restrict the value to be strictly positive Signed-off-by: Rouven Bauer <[email protected]> Co-authored-by: Andy Heap <[email protected]>
1 parent 6da0a46 commit 7b07121

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
3737
- `neo4j.exceptions.UnsupportedServerProduct` if no common bolt protocol version could be negotiated with the server
3838
(instead of internal `neo4j._exceptions.BoltHandshakeError`).
3939
`UnsupportedServerProduct` is now a subclass of `ServiceUnavailable` (instead of `Exception` directly).
40+
- `connection_acquisition_timeout` configuration option
41+
- `ValueError` on invalid values (instead of `ClientError`)
42+
- Consistently restrict the value to be strictly positive
4043

4144

4245
## Version 5.28

src/neo4j/_async/io/_pool.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import abc
2020
import asyncio
2121
import logging
22+
import math
2223
import typing as t
2324
from collections import (
2425
defaultdict,
@@ -670,6 +671,7 @@ async def acquire(
670671
# The access_mode and database is not needed for a direct connection,
671672
# it's just there for consistency.
672673
access_mode = check_access_mode(access_mode)
674+
_check_acquisition_timeout(timeout)
673675
log.debug(
674676
"[#0000] _: <POOL> acquire direct connection, "
675677
"access_mode=%r, database=%r",
@@ -972,6 +974,7 @@ async def update_routing_table(
972974
973975
:raise neo4j.exceptions.ServiceUnavailable:
974976
"""
977+
_check_acquisition_timeout(acquisition_timeout)
975978
async with self.refresh_lock:
976979
routing_table = await self.get_routing_table(database)
977980
if routing_table is not None:
@@ -1149,11 +1152,7 @@ async def acquire(
11491152
database_callback=None,
11501153
):
11511154
access_mode = check_access_mode(access_mode)
1152-
if not timeout:
1153-
# TODO: 6.0 - change this to be a ValueError
1154-
raise ClientError(
1155-
f"'timeout' must be a float larger than 0; {timeout}"
1156-
)
1155+
_check_acquisition_timeout(timeout)
11571156

11581157
target_database = database.name
11591158

@@ -1246,3 +1245,17 @@ async def on_write_failure(self, address, database):
12461245
if table is not None:
12471246
table.writers.discard(address)
12481247
log.debug("[#0000] _: <POOL> table=%r", self.routing_tables)
1248+
1249+
1250+
def _check_acquisition_timeout(timeout: object) -> None:
1251+
if not isinstance(timeout, (int, float)):
1252+
raise TypeError(
1253+
"Connection acquisition timeout must be a number, "
1254+
f"got {type(timeout)}"
1255+
)
1256+
if timeout <= 0:
1257+
raise ValueError(
1258+
f"Connection acquisition timeout must be > 0, got {timeout}"
1259+
)
1260+
if math.isnan(timeout):
1261+
raise ValueError("Connection acquisition timeout must not be NaN")

src/neo4j/_sync/io/_pool.py

Lines changed: 18 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testkitbackend/_async/requests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,15 @@ async def forced_routing_table_update(backend, data):
981981
driver = backend.drivers[driver_id]
982982
database = data["database"]
983983
bookmarks = data["bookmarks"]
984+
acquisition_timeout = (
985+
driver._default_workspace_config.connection_acquisition_timeout
986+
)
984987
async with driver._pool.refresh_lock:
985988
await driver._pool.update_routing_table(
986-
database=database, imp_user=None, bookmarks=bookmarks
989+
database=database,
990+
imp_user=None,
991+
bookmarks=bookmarks,
992+
acquisition_timeout=acquisition_timeout,
987993
)
988994
await backend.send_response("Driver", {"id": driver_id})
989995

testkitbackend/_sync/requests.py

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)