From cad97f430aa595937a96e18882d55e703689809d Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 8 Jul 2024 00:36:42 +0200 Subject: [PATCH] dispose engine --- cashu/core/db.py | 21 ++++++--------------- tests/conftest.py | 2 ++ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/cashu/core/db.py b/cashu/core/db.py index 18b164a1..a677ee69 100644 --- a/cashu/core/db.py +++ b/cashu/core/db.py @@ -11,7 +11,7 @@ from sqlalchemy import text from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker -from sqlalchemy.pool import NullPool +from sqlalchemy.pool import NullPool, QueuePool from cashu.core.settings import settings @@ -163,6 +163,10 @@ def _parse_timestamp(value, _): kwargs = {} if not settings.db_connection_pool: kwargs["poolclass"] = NullPool + else: + kwargs["poolclass"] = QueuePool + kwargs["pool_size"] = 50 + kwargs["max_overflow"] = 100 self.engine = create_async_engine(database_uri, **kwargs) self.async_session = sessionmaker( @@ -208,19 +212,6 @@ async def connect( lock_select_statement: Optional[str] = None, lock_timeout: Optional[float] = None, ): - # inherited = True - - # if self._connection is None: - # await self.initialize_connection() - # assert self._connection is not None, "Connection not initialized" - # inherited = False - - # if self._connection.in_transaction(): - # logger.trace("Connection is in transaction. Creating new session") - # return - # else: - # session = self._connection - async def _handle_lock_retry(retry_delay, timeout, start_time) -> float: await asyncio.sleep(retry_delay) retry_delay = min(retry_delay * 2, timeout - (time.time() - start_time)) @@ -231,7 +222,7 @@ def _is_lock_exception(e): logger.trace(f"Lock exception: {e}") return True - timeout = 10 # lock_timeout or 5 # default to 5 seconds + timeout = lock_timeout or 5 # default to 5 seconds start_time = time.time() retry_delay = 0.1 random_int = int(time.time() * 1000) diff --git a/tests/conftest.py b/tests/conftest.py index 12d777ce..d7f76d79 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -129,6 +129,7 @@ async def start_mint_init(ledger: Ledger) -> Ledger: # drop all tables await conn.execute("DROP SCHEMA public CASCADE;") await conn.execute("CREATE SCHEMA public;") + await db.engine.dispose() wallets_module = importlib.import_module("cashu.lightning") lightning_backend = getattr(wallets_module, settings.mint_backend_bolt11_sat)() @@ -145,6 +146,7 @@ async def start_mint_init(ledger: Ledger) -> Ledger: ledger = await start_mint_init(ledger) yield ledger print("teardown") + await ledger.db.engine.dispose() # # This fixture is used for tests that require API access to the mint