Skip to content
New issue

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

By clicking “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? Sign in to your account

Add the Pool's available_connections property #122

Closed
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions asynch/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading