]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor(pool): move null pool base to its own module
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 5 Oct 2023 01:58:57 +0000 (03:58 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 11 Oct 2023 21:45:39 +0000 (23:45 +0200)
And some more light refactoring.

psycopg_pool/psycopg_pool/base_null_pool.py [new file with mode: 0644]
psycopg_pool/psycopg_pool/null_pool.py
psycopg_pool/psycopg_pool/null_pool_async.py

diff --git a/psycopg_pool/psycopg_pool/base_null_pool.py b/psycopg_pool/psycopg_pool/base_null_pool.py
new file mode 100644 (file)
index 0000000..199e701
--- /dev/null
@@ -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
index 09f66bda5b350ec3fb82ce1b9a7686ef964cc162..68945fb4cae89b0d2116dfc57360f3209d4fd313 100644 (file)
@@ -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
index b60370c53b48780ad8b94ba7a8beb413d83c9028..2407c60dec8ae2dab3573d57eee2a6eaba59d4dd 100644 (file)
@@ -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: