]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor(pool): generate null pool module from async counterpart
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 5 Oct 2023 02:06:34 +0000 (04:06 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 11 Oct 2023 21:45:39 +0000 (23:45 +0200)
psycopg_pool/psycopg_pool/null_pool.py
psycopg_pool/psycopg_pool/null_pool_async.py
tools/async_to_sync.py
tools/convert_async_to_sync.sh

index 68945fb4cae89b0d2116dfc57360f3209d4fd313..f8cdb9f5512e3ae26eb6c5fe4fd2a6e73df09b1e 100644 (file)
@@ -1,3 +1,6 @@
+# WARNING: this file is auto-generated by 'async_to_sync.py'
+# from the original file 'null_pool_async.py'
+# DO NOT CHANGE! Change the original file instead.
 """
 Psycopg null connection pool module.
 """
@@ -14,11 +17,11 @@ from psycopg.pq import TransactionStatus
 from psycopg.rows import TupleRow
 
 from .abc import CT, ConnectionCB, ConnectFailedCB
-from .pool import ConnectionPool, AddConnection
 from .errors import PoolTimeout, TooManyRequests
 from ._compat import ConnectionTimeout
 from ._acompat import Event
 from .base_null_pool import _BaseNullConnectionPool
+from .pool import ConnectionPool, AddConnection
 
 logger = logging.getLogger("psycopg.pool")
 
@@ -78,7 +81,6 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]):
         configure: Optional[ConnectionCB[CT]] = None,
         reset: Optional[ConnectionCB[CT]] = None,
         kwargs: Optional[Dict[str, Any]] = None,
-        # Note: default value changed to 0.
         min_size: int = 0,
         max_size: Optional[int] = None,
         name: Optional[str] = None,
@@ -89,7 +91,7 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]):
         reconnect_timeout: float = 5 * 60.0,
         reconnect_failed: Optional[ConnectFailedCB] = None,
         num_workers: int = 3,
-    ):
+    ):  # Note: min_size default value changed to 0.
         super().__init__(
             conninfo,
             open=open,
@@ -128,7 +130,7 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]):
         logger.info("waiting for pool %r initialization", self.name)
         self.run_task(AddConnection(self))
         if not self._pool_full_event.wait(timeout):
-            self.close()  # stop all the threads
+            self.close()  # stop all the tasks
             raise PoolTimeout(f"pool initialization incomplete after {timeout} sec")
 
         with self._lock:
@@ -146,16 +148,18 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]):
             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(
                 f"the pool {self.name!r} has already"
-                f" {len(self._waiting)} requests waiting"
+                f" {len(self._waiting)} requests waiting"
             )
         return conn
 
     def _maybe_close_connection(self, conn: CT) -> bool:
+        # Close the connection if no client is waiting for it, or if the pool
+        # is closed. For extra refcare remove the pool reference from it.
+        # Maintain the stats.
         with self._lock:
             if not self._closed and self._waiting:
                 return False
@@ -172,7 +176,7 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]):
 
         Only *max_size* can be changed; *min_size* must remain 0.
         """
-        min_size, max_size = self._check_size(min_size, max_size)
+        (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
@@ -203,7 +207,6 @@ class NullConnectionPool(_BaseNullConnectionPool, ConnectionPool[CT]):
             else:
                 # No client waiting for a connection: close the connection
                 conn.close()
-
                 # If we have been asked to wait for pool init, notify the
                 # waiter if the pool is ready.
                 if self._pool_full_event:
index 2407c60dec8ae2dab3573d57eee2a6eaba59d4dd..bc3931c984144f715021d296738c8015f32ee451 100644 (file)
@@ -78,8 +78,7 @@ class AsyncNullConnectionPool(_BaseNullConnectionPool, AsyncConnectionPool[ACT])
         configure: Optional[AsyncConnectionCB[ACT]] = None,
         reset: Optional[AsyncConnectionCB[ACT]] = None,
         kwargs: Optional[Dict[str, Any]] = None,
-        # Note: default value changed to 0.
-        min_size: int = 0,
+        min_size: int = 0,  # Note: min_size default value changed to 0.
         max_size: Optional[int] = None,
         name: Optional[str] = None,
         timeout: float = 30.0,
@@ -151,7 +150,7 @@ class AsyncNullConnectionPool(_BaseNullConnectionPool, AsyncConnectionPool[ACT])
             self._stats[self._REQUESTS_ERRORS] += 1
             raise TooManyRequests(
                 f"the pool {self.name!r} has already"
-                f" {len(self._waiting)} requests waiting"
+                f" {len(self._waiting)} requests waiting"
             )
         return conn
 
index f0bb56bab1db7f819abb5fd24fbf0831c4212fbe..300fc0ab07522303b8f42948fc51497308213f5a 100755 (executable)
@@ -199,6 +199,7 @@ class RenameAsyncToSync(ast.NodeTransformer):
         "cursor_async": "cursor",
         "ensure_table_async": "ensure_table",
         "find_insert_problem_async": "find_insert_problem",
+        "pool_async": "pool",
         "psycopg_pool.pool_async": "psycopg_pool.pool",
         "psycopg_pool.sched_async": "psycopg_pool.sched",
         "sched_async": "sched",
index e7976d3d2ec29f33f6170a201322a3e1e44af1d3..19dfbe11189996afa7fc6b14420d2a6242daaa71 100755 (executable)
@@ -20,6 +20,7 @@ outputs=""
 for async in \
     psycopg/psycopg/connection_async.py \
     psycopg/psycopg/cursor_async.py \
+    psycopg_pool/psycopg_pool/null_pool_async.py \
     psycopg_pool/psycopg_pool/pool_async.py \
     psycopg_pool/psycopg_pool/sched_async.py \
     tests/pool/test_pool_async.py \