From: Daniele Varrazzo Date: Thu, 5 Oct 2023 02:06:34 +0000 (+0200) Subject: refactor(pool): generate null pool module from async counterpart X-Git-Tag: pool-3.2.0~12^2~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=23a9200af97f5183f371daeebe91ea139895416b;p=thirdparty%2Fpsycopg.git refactor(pool): generate null pool module from async counterpart --- diff --git a/psycopg_pool/psycopg_pool/null_pool.py b/psycopg_pool/psycopg_pool/null_pool.py index 68945fb4c..f8cdb9f55 100644 --- a/psycopg_pool/psycopg_pool/null_pool.py +++ b/psycopg_pool/psycopg_pool/null_pool.py @@ -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: diff --git a/psycopg_pool/psycopg_pool/null_pool_async.py b/psycopg_pool/psycopg_pool/null_pool_async.py index 2407c60de..bc3931c98 100644 --- a/psycopg_pool/psycopg_pool/null_pool_async.py +++ b/psycopg_pool/psycopg_pool/null_pool_async.py @@ -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 diff --git a/tools/async_to_sync.py b/tools/async_to_sync.py index f0bb56bab..300fc0ab0 100755 --- a/tools/async_to_sync.py +++ b/tools/async_to_sync.py @@ -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", diff --git a/tools/convert_async_to_sync.sh b/tools/convert_async_to_sync.sh index e7976d3d2..19dfbe111 100755 --- a/tools/convert_async_to_sync.sh +++ b/tools/convert_async_to_sync.sh @@ -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 \