Skip to content

Commit 51fd4a4

Browse files
authored
[4.4] Fix #796: connection pool clogging up (#803)
The reservation count was also increased if the pool was full and the reservation would never be turned into a new connection. This lead to the pool clogging up after a while. Add unit tests
1 parent 2ecc490 commit 51fd4a4

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

neo4j/io/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,9 @@ def connection_creator():
756756
+ self.connections_reservations[address])
757757
can_create_new_connection = (infinite_pool_size
758758
or pool_size < max_pool_size)
759-
self.connections_reservations[address] += 1
760-
if can_create_new_connection:
761-
return connection_creator
759+
if can_create_new_connection:
760+
self.connections_reservations[address] += 1
761+
return connection_creator
762762

763763
def _acquire(self, address, deadline):
764764

tests/unit/io/test_neo4j_pool.py

+27
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
RoutingConfig,
3636
WorkspaceConfig
3737
)
38+
from neo4j._deadline import Deadline
3839
from neo4j.exceptions import (
3940
ServiceUnavailable,
4041
SessionExpired
@@ -271,3 +272,29 @@ def test_failing_opener_leaves_connections_in_use_alone(opener):
271272
pool.acquire(READ_ACCESS, 30, 60, "test_db", None)
272273

273274
assert not cx1.closed()
275+
276+
277+
def test__acquire_new_later_with_room(opener):
278+
config = PoolConfig()
279+
config.max_connection_pool_size = 1
280+
pool = Neo4jPool(
281+
opener, config, WorkspaceConfig(), ROUTER_ADDRESS
282+
)
283+
assert pool.connections_reservations[READER_ADDRESS] == 0
284+
creator = pool._acquire_new_later(READER_ADDRESS, Deadline(1))
285+
assert pool.connections_reservations[READER_ADDRESS] == 1
286+
assert callable(creator)
287+
288+
289+
def test__acquire_new_later_without_room(opener):
290+
config = PoolConfig()
291+
config.max_connection_pool_size = 1
292+
pool = Neo4jPool(
293+
opener, config, WorkspaceConfig(), ROUTER_ADDRESS
294+
)
295+
_ = pool.acquire(READ_ACCESS, 30, 60, "test_db", None)
296+
# pool is full now
297+
assert pool.connections_reservations[READER_ADDRESS] == 0
298+
creator = pool._acquire_new_later(READER_ADDRESS, Deadline(1))
299+
assert pool.connections_reservations[READER_ADDRESS] == 0
300+
assert creator is None

0 commit comments

Comments
 (0)