long2ice / asynch

An asyncio ClickHouse Python Driver with native (TCP) interface support.
https://github.com/long2ice/asynch
Apache License 2.0
185 stars 43 forks source link

Add the Pool's available_connections property #122

Closed stankudrow closed 1 month ago

stankudrow commented 1 month ago

The pool.available_connections property shows the number of connections one can request from the pool.

If a pool object is requested to give out a connection when the number of acquired connections is equal to the pool.maxsize property, the AsynchPoolError is raised because the pool cannot give birth to a connection when its capacities are exceeded.

A possible use case:

async with pool:
    # some code
    if pool.availlable_connections:
        conn_ctx_manager = pool.connection()
       # one can safely request a connection from the pool
    else:
        # requesting a connection from the pool will lead to the AsynchPoolError

The source of inspiration: the issue #121 by @itssimon .

stankudrow commented 1 month ago

I think these Pool properties are error-prone and are better to be replaced with async methods, e.g., get_free_connections() with async with self._lock context.

Perhaps this PR should be closed and in favour of the PR with properties replaced with corresponding coro methods for ensuring the coroutine-safety.