Skip to content

Commit

Permalink
dispose engine
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Jul 7, 2024
1 parent f2ba89c commit cad97f4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
21 changes: 6 additions & 15 deletions cashu/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand All @@ -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
Expand Down

0 comments on commit cad97f4

Please sign in to comment.