From: Daniele Varrazzo Date: Thu, 5 Oct 2023 01:58:57 +0000 (+0200) Subject: refactor(pool): move null pool base to its own module X-Git-Tag: pool-3.2.0~12^2~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9cf729a43fb5a4136b2c0800d49195d31dd65530;p=thirdparty%2Fpsycopg.git refactor(pool): move null pool base to its own module And some more light refactoring. --- diff --git a/psycopg_pool/psycopg_pool/base_null_pool.py b/psycopg_pool/psycopg_pool/base_null_pool.py new file mode 100644 index 000000000..199e701d2 --- /dev/null +++ b/psycopg_pool/psycopg_pool/base_null_pool.py @@ -0,0 +1,29 @@ +""" +Psycopg mixin class null connection pools +""" + +# Copyright (C) 2022 The Psycopg Team + +from __future__ import annotations + + +class _BaseNullConnectionPool: + def _check_size(self, min_size: int, max_size: int | None) -> tuple[int, int]: + if max_size is None: + max_size = min_size + + if min_size != 0: + raise ValueError("null pools must have min_size = 0") + if max_size < min_size: + raise ValueError("max_size must be greater or equal than min_size") + + return min_size, max_size + + def _start_initial_tasks(self) -> None: + # Null pools don't have background tasks to fill connections + # or to grow/shrink. + return + + def _maybe_grow_pool(self) -> None: + # null pools don't grow + pass diff --git a/psycopg_pool/psycopg_pool/null_pool.py b/psycopg_pool/psycopg_pool/null_pool.py index 09f66bda5..68945fb4c 100644 --- a/psycopg_pool/psycopg_pool/null_pool.py +++ b/psycopg_pool/psycopg_pool/null_pool.py @@ -1,5 +1,5 @@ """ -Psycopg null connection pools +Psycopg null connection pool module. """ # Copyright (C) 2022 The Psycopg Team @@ -7,7 +7,7 @@ Psycopg null connection pools from __future__ import annotations import logging -from typing import Any, cast, Dict, Optional, overload, Tuple, Type +from typing import Any, cast, Dict, Optional, overload, Type from psycopg import Connection from psycopg.pq import TransactionStatus @@ -18,32 +18,11 @@ from .pool import ConnectionPool, AddConnection from .errors import PoolTimeout, TooManyRequests from ._compat import ConnectionTimeout from ._acompat import Event +from .base_null_pool import _BaseNullConnectionPool logger = logging.getLogger("psycopg.pool") -class _BaseNullConnectionPool: - def _check_size(self, min_size: int, max_size: Optional[int]) -> Tuple[int, int]: - if max_size is None: - max_size = min_size - - if min_size != 0: - raise ValueError("null pools must have min_size = 0") - if max_size < min_size: - raise ValueError("max_size must be greater or equal than min_size") - - return min_size, max_size - - def _start_initial_tasks(self) -> None: - # Null pools don't have background tasks to fill connections - # or to grow/shrink. - return - - def _maybe_grow_pool(self) -> None: - # null pools don't grow - pass - - class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]): @overload def __init__( @@ -196,10 +175,7 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]): min_size, max_size = self._check_size(min_size, max_size) logger.info( - "resizing %r to min_size=%s max_size=%s", - self.name, - min_size, - max_size, + "resizing %r to min_size=%s max_size=%s", self.name, min_size, max_size ) with self._lock: self._min_size = min_size diff --git a/psycopg_pool/psycopg_pool/null_pool_async.py b/psycopg_pool/psycopg_pool/null_pool_async.py index b60370c53..2407c60de 100644 --- a/psycopg_pool/psycopg_pool/null_pool_async.py +++ b/psycopg_pool/psycopg_pool/null_pool_async.py @@ -1,5 +1,5 @@ """ -psycopg asynchronous null connection pool +Psycopg null connection pool module. """ # Copyright (C) 2022 The Psycopg Team @@ -17,7 +17,7 @@ from .abc import ACT, AsyncConnectionCB, AsyncConnectFailedCB from .errors import PoolTimeout, TooManyRequests from ._compat import ConnectionTimeout from ._acompat import AEvent -from .null_pool import _BaseNullConnectionPool +from .base_null_pool import _BaseNullConnectionPool from .pool_async import AsyncConnectionPool, AddConnection logger = logging.getLogger("psycopg.pool") @@ -109,6 +109,16 @@ class AsyncNullConnectionPool(_BaseNullConnectionPool, AsyncConnectionPool[ACT]) ) async def wait(self, timeout: float = 30.0) -> None: + """ + Create a connection for test. + + Calling this function will verify that the connectivity with the + database works as expected. However the connection will not be stored + in the pool. + + Close the pool, and raise `PoolTimeout`, if not ready within *timeout* + sec. + """ self._check_open_getconn() async with self._lock: @@ -136,6 +146,7 @@ class AsyncNullConnectionPool(_BaseNullConnectionPool, AsyncConnectionPool[ACT]) except ConnectionTimeout as ex: raise PoolTimeout(str(ex)) from None self._nconns += 1 + elif self.max_waiting and len(self._waiting) >= self.max_waiting: self._stats[self._REQUESTS_ERRORS] += 1 raise TooManyRequests( @@ -160,19 +171,21 @@ class AsyncNullConnectionPool(_BaseNullConnectionPool, AsyncConnectionPool[ACT]) return True async def resize(self, min_size: int, max_size: Optional[int] = None) -> None: + """Change the size of the pool during runtime. + + Only *max_size* can be changed; *min_size* must remain 0. + """ min_size, max_size = self._check_size(min_size, max_size) logger.info( - "resizing %r to min_size=%s max_size=%s", - self.name, - min_size, - max_size, + "resizing %r to min_size=%s max_size=%s", self.name, min_size, max_size ) async with self._lock: self._min_size = min_size self._max_size = max_size async def check(self) -> None: + """No-op, as the pool doesn't have connections in its state.""" pass async def _add_to_pool(self, conn: ACT) -> None: