From da742fc11a3d78dba2eb9561002df1d42c119226 Mon Sep 17 00:00:00 2001 From: Stanley Kudrow Date: Sun, 20 Oct 2024 21:01:36 +0300 Subject: [PATCH 1/2] add the Pool's available_connections property --- asynch/pool.py | 14 ++++++++++++++ tests/test_pool.py | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/asynch/pool.py b/asynch/pool.py index a62e705..0b2890d 100644 --- a/asynch/pool.py +++ b/asynch/pool.py @@ -214,6 +214,20 @@ def acquired_connections(self) -> int: return len(self._acquired_connections) + @property + def available_connections(self) -> int: + """Returns the number of available connections in the pool. + + The number of available connections means + those that can be acquired from the pool. + Equivalent to `pool.maxsize - pool.connections`. + + :return: the number of connections available to be requested from the pool + :rtype: int + """ + + return self.maxsize - self.connections + @property def free_connections(self) -> int: """Returns the number of free connections in the pool. diff --git a/tests/test_pool.py b/tests/test_pool.py index 070a4dd..dd796fe 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -59,24 +59,29 @@ async def test_pool_connection_attributes(config): assert pool.connections == 0 assert pool.free_connections == 0 assert pool.acquired_connections == 0 + assert pool.available_connections == constants.POOL_MAX_SIZE async with pool: assert pool.connections == constants.POOL_MIN_SIZE assert pool.free_connections == constants.POOL_MIN_SIZE assert pool.acquired_connections == 0 + assert pool.available_connections == (constants.POOL_MAX_SIZE - constants.POOL_MIN_SIZE) async with pool.connection(): assert pool.connections == constants.POOL_MIN_SIZE assert pool.free_connections == 0 assert pool.acquired_connections == constants.POOL_MIN_SIZE + assert pool.available_connections == (constants.POOL_MAX_SIZE - constants.POOL_MIN_SIZE) assert pool.connections == constants.POOL_MIN_SIZE assert pool.free_connections == constants.POOL_MIN_SIZE assert pool.acquired_connections == 0 + assert pool.available_connections == (constants.POOL_MAX_SIZE - constants.POOL_MIN_SIZE) assert pool.connections == 0 assert pool.free_connections == 0 assert pool.acquired_connections == 0 + assert pool.available_connections == constants.POOL_MAX_SIZE @pytest.mark.asyncio From 1c88fc4160fa64030b555612177d86b610454f51 Mon Sep 17 00:00:00 2001 From: Stanley Kudrow Date: Sun, 20 Oct 2024 21:05:48 +0300 Subject: [PATCH 2/2] add the PR #122 record --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 630d5dd..93f4249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.2 +### 0.2.6 + +- Add the `available_connections` property to the `Pool` class. The issue #121 by @itssimon. By @stankudrow in #122 + ### 0.2.5 - Add more validation rules in the `parse_dsn` function. By @stankudrow in #113