And some more light refactoring.
--- /dev/null
+"""
+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
"""
-Psycopg null connection pools
+Psycopg null connection pool module.
"""
# Copyright (C) 2022 The Psycopg Team
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
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__(
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
"""
-psycopg asynchronous null connection pool
+Psycopg null connection pool module.
"""
# Copyright (C) 2022 The Psycopg Team
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")
)
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:
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(
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: