Skip to content

Commit 603e386

Browse files
authored
Add Pool methods to determine its min, max, current and idle size (#849)
The new `Pool.get_size()`, `Pool.get_min_size()`, `Pool.get_max_size()`, `Pool.get_idle_size()` methods are added to get the size of the current, minimum, maximum and idle connection set size at any given moment.
1 parent 4d39a05 commit 603e386

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

asyncpg/pool.py

+34
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def __init__(self, pool, *, max_queries, setup, max_inactive_time):
118118
self._timeout = None
119119
self._generation = None
120120

121+
def is_connected(self):
122+
return self._con is not None and not self._con.is_closed()
123+
124+
def is_idle(self):
125+
return not self._in_use
126+
121127
async def connect(self):
122128
if self._con is not None:
123129
raise exceptions.InternalClientError(
@@ -444,6 +450,34 @@ async def _initialize(self):
444450

445451
await asyncio.gather(*connect_tasks)
446452

453+
def get_size(self):
454+
"""Return the current number of connections in this pool.
455+
456+
.. versionadded:: 0.25.0
457+
"""
458+
return sum(h.is_connected() for h in self._holders)
459+
460+
def get_min_size(self):
461+
"""Return the minimum number of connections in this pool.
462+
463+
.. versionadded:: 0.25.0
464+
"""
465+
return self._minsize
466+
467+
def get_max_size(self):
468+
"""Return the maximum allowed number of connections in this pool.
469+
470+
.. versionadded:: 0.25.0
471+
"""
472+
return self._maxsize
473+
474+
def get_idle_size(self):
475+
"""Return the current number of idle connections in this pool.
476+
477+
.. versionadded:: 0.25.0
478+
"""
479+
return sum(h.is_connected() and h.is_idle() for h in self._holders)
480+
447481
def set_connect_args(self, dsn=None, **connect_kwargs):
448482
r"""Set the new connection arguments for this pool.
449483

tests/test_pool.py

+21
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,27 @@ async def test_pool_handles_inactive_connection_errors(self):
720720

721721
await pool.close()
722722

723+
async def test_pool_size_and_capacity(self):
724+
async with self.create_pool(
725+
database='postgres',
726+
min_size=2,
727+
max_size=3,
728+
) as pool:
729+
self.assertEqual(pool.get_min_size(), 2)
730+
self.assertEqual(pool.get_max_size(), 3)
731+
self.assertEqual(pool.get_size(), 2)
732+
self.assertEqual(pool.get_idle_size(), 2)
733+
734+
async with pool.acquire():
735+
self.assertEqual(pool.get_idle_size(), 1)
736+
737+
async with pool.acquire():
738+
self.assertEqual(pool.get_idle_size(), 0)
739+
740+
async with pool.acquire():
741+
self.assertEqual(pool.get_size(), 3)
742+
self.assertEqual(pool.get_idle_size(), 0)
743+
723744
@unittest.skipIf(sys.version_info[:2] < (3, 6), 'no asyncgen support')
724745
async def test_pool_handles_transaction_exit_in_asyncgen_1(self):
725746
pool = await self.create_pool(database='postgres',

0 commit comments

Comments
 (0)