Skip to content

Add Pool methods to determine its min, max, current and idle size #849

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions asyncpg/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def __init__(self, pool, *, max_queries, setup, max_inactive_time):
self._timeout = None
self._generation = None

def is_connected(self):
return self._con is not None and not self._con.is_closed()

def is_idle(self):
return not self._in_use

async def connect(self):
if self._con is not None:
raise exceptions.InternalClientError(
Expand Down Expand Up @@ -444,6 +450,34 @@ async def _initialize(self):

await asyncio.gather(*connect_tasks)

def get_size(self):
"""Return the current number of connections in this pool.

.. versionadded:: 0.25.0
"""
return sum(h.is_connected() for h in self._holders)

def get_min_size(self):
"""Return the minimum number of connections in this pool.

.. versionadded:: 0.25.0
"""
return self._minsize

def get_max_size(self):
"""Return the maximum allowed number of connections in this pool.

.. versionadded:: 0.25.0
"""
return self._maxsize

def get_idle_size(self):
"""Return the current number of idle connections in this pool.

.. versionadded:: 0.25.0
"""
return sum(h.is_connected() and h.is_idle() for h in self._holders)

def set_connect_args(self, dsn=None, **connect_kwargs):
r"""Set the new connection arguments for this pool.

Expand Down
21 changes: 21 additions & 0 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,27 @@ async def test_pool_handles_inactive_connection_errors(self):

await pool.close()

async def test_pool_size_and_capacity(self):
async with self.create_pool(
database='postgres',
min_size=2,
max_size=3,
) as pool:
self.assertEqual(pool.get_min_size(), 2)
self.assertEqual(pool.get_max_size(), 3)
self.assertEqual(pool.get_size(), 2)
self.assertEqual(pool.get_idle_size(), 2)

async with pool.acquire():
self.assertEqual(pool.get_idle_size(), 1)

async with pool.acquire():
self.assertEqual(pool.get_idle_size(), 0)

async with pool.acquire():
self.assertEqual(pool.get_size(), 3)
self.assertEqual(pool.get_idle_size(), 0)

@unittest.skipIf(sys.version_info[:2] < (3, 6), 'no asyncgen support')
async def test_pool_handles_transaction_exit_in_asyncgen_1(self):
pool = await self.create_pool(database='postgres',
Expand Down