| 1 |
import asyncio |
| 2 |
from contextlib import asynccontextmanager |
| 3 |
from typing import Any, AsyncGenerator, Optional |
| 4 |
|
| 5 |
from ..pool import ConnectionPool, ClosedPool, EmptyPool |
| 6 |
|
| 7 |
from .aioconnection import AIOLDAPConnection |
| 8 |
|
| 9 |
MYPY = False |
| 10 |
|
| 11 |
if MYPY: |
| 12 |
from ..ldapclient import LDAPClient |
| 13 |
|
| 14 |
|
| 15 |
class AIOConnectionPool(ConnectionPool[AIOLDAPConnection]): |
| 16 |
""" |
| 17 |
A connection pool that can be used with asnycio tasks. It's inherited from |
| 18 |
:class:`bonsai.pool.ConnectionPool`. |
| 19 |
|
| 20 |
:param LDAPClient client: the :class:`bonsai.LDAPClient` that's used to create |
| 21 |
connections. |
| 22 |
:param int minconn: the minimum number of connections that's created |
| 23 |
after the pool is opened. |
| 24 |
:param int maxconn: the maximum number of connections in the pool. |
| 25 |
:param \\*\\*kwargs: additional keyword arguments that are passed to |
| 26 |
the :meth:`bonsai.LDAPClient.connect` method. |
| 27 |
:raises ValueError: when the minconn is negative or the maxconn is less |
| 28 |
than the minconn. |
| 29 |
""" |
| 30 |
|
| 31 |
def __init__( |
| 32 |
self, |
| 33 |
client: "LDAPClient", |
| 34 |
minconn: int = 1, |
| 35 |
maxconn: int = 10, |
| 36 |
loop: Optional[asyncio.AbstractEventLoop] = None, |
| 37 |
**kwargs: Any |
| 38 |
): |
| 39 |
super().__init__(client, minconn, maxconn, **kwargs) |
| 40 |
self._loop = loop |
| 41 |
try: |
| 42 |
|
| 43 |
|
| 44 |
self._lock = asyncio.Condition(loop=self._loop) |
| 45 |
except TypeError: |
| 46 |
self._lock = asyncio.Condition() |
| 47 |
|
| 48 |
async def open(self) -> None: |
| 49 |
async with self._lock: |
| 50 |
for _ in range( |
| 51 |
self._minconn - self.idle_connection - self.shared_connection |
| 52 |
): |
| 53 |
conn = await self._client.connect( |
| 54 |
is_async=True, loop=self._loop, **self._kwargs |
| 55 |
) |
| 56 |
self._idles.add(conn) |
| 57 |
self._closed = False |
| 58 |
|
| 59 |
async def get(self) -> AIOLDAPConnection: |
| 60 |
async with self._lock: |
| 61 |
if self._closed: |
| 62 |
raise ClosedPool("The pool is closed.") |
| 63 |
await self._lock.wait_for(lambda: not self.empty or self._closed) |
| 64 |
try: |
| 65 |
conn = self._idles.pop() |
| 66 |
except KeyError: |
| 67 |
if len(self._used) < self._maxconn: |
| 68 |
conn = await self._client.connect( |
| 69 |
is_async=True, loop=self._loop, **self._kwargs |
| 70 |
) |
| 71 |
else: |
| 72 |
raise EmptyPool("Pool is empty.") from None |
| 73 |
self._used.add(conn) |
| 74 |
self._lock.notify() |
| 75 |
return conn |
| 76 |
|
| 77 |
async def put(self, conn: AIOLDAPConnection) -> None: |
| 78 |
async with self._lock: |
| 79 |
super().put(conn) |
| 80 |
self._lock.notify() |
| 81 |
|
| 82 |
async def close(self) -> None: |
| 83 |
async with self._lock: |
| 84 |
super().close() |
| 85 |
self._lock.notify_all() |
| 86 |
|
| 87 |
@asynccontextmanager |
| 88 |
async def spawn( |
| 89 |
self, *args: Any, **kwargs: Any |
| 90 |
) -> AsyncGenerator[AIOLDAPConnection, None]: |
| 91 |
conn = None |
| 92 |
try: |
| 93 |
if self._closed: |
| 94 |
await self.open() |
| 95 |
conn = await self.get(*args, **kwargs) |
| 96 |
yield conn |
| 97 |
finally: |
| 98 |
if conn: |
| 99 |
await self.put(conn) |