Skip to content

Commit

Permalink
🎨 Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
ff137 committed Feb 12, 2025
1 parent 943125d commit 5d991da
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions acapy_agent/ledger/indy_vdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import os.path
import tempfile
import traceback
from datetime import date, datetime, timezone
from io import StringIO
from pathlib import Path
Expand Down Expand Up @@ -149,16 +148,21 @@ async def get_or_create(
"""
LOGGER.debug(
"Creating or retrieving IndyVdrLedgerPool instance with params: name=%s, "
"keepalive=%s, cache_duration=%s, read_only=%s\nTraceback:\n%s",
"keepalive=%s, cache_duration=%s, read_only=%s",
name,
keepalive,
cache_duration,
read_only,
"".join(traceback.format_stack(limit=5)),
)
traceback.print_stack(limit=10)

config_key = (name, keepalive, cache_duration, read_only, socks_proxy)
config_key = (
name,
keepalive,
cache_duration,
genesis_transactions,
read_only,
socks_proxy,
)
LOGGER.debug("Generated config key: %s", config_key)

async with cls._lock:
Expand Down Expand Up @@ -229,27 +233,23 @@ async def release_instance(cls, instance: "IndyVdrLedgerPool") -> None:
LOGGER.debug("Generated config key for release: %s", config_key)

async with cls._lock:
async with instance.ref_lock:
instance.ref_count -= 1
LOGGER.debug(
"Decremented reference count to %s for instance %s",
instance.ref_count,
config_key,
)
if instance.ref_count <= 0:
LOGGER.debug("Reference count is zero or negative, cleaning up instance")
await instance._close()
del cls._instances[config_key]
LOGGER.debug(
"Decremented reference count to %s for instance %s",
instance.ref_count,
"Successfully removed IndyVdrLedgerPool instance: %s",
config_key,
)
if instance.ref_count <= 0:
LOGGER.debug(
"Reference count is zero or negative, cleaning up instance"
)
await instance._close()
del cls._instances[config_key]
LOGGER.debug(
"Successfully removed IndyVdrLedgerPool instance: %s",
config_key,
)
else:
LOGGER.debug(
"Instance still has active references: %s", instance.ref_count
)
else:
LOGGER.debug(
"Instance still has active references: %s", instance.ref_count
)

@property
def cfg_path(self) -> Path:
Expand Down Expand Up @@ -437,9 +437,9 @@ async def _keepalive_closer(timeout: int) -> None:

await IndyVdrLedgerPool.release_instance(self)
except Exception as e:
LOGGER.exception(
"Exception occurred in closer coroutine during pool closure.",
exc_info=e,
LOGGER.error(
"Exception occurred in closer coroutine during pool closure: %s",
e,
)

async with self.ref_lock:
Expand Down Expand Up @@ -514,22 +514,14 @@ async def __aenter__(self) -> "IndyVdrLedger":
The current instance
"""
LOGGER.debug(
"Entering IndyVdrLedger context manager\nTraceback:\n%s",
"".join(traceback.format_stack(limit=5)),
)
traceback.print_stack(limit=10)
LOGGER.debug("Entering IndyVdrLedger context manager")
await super().__aenter__()
await self.pool.context_open()
return self

async def __aexit__(self, exc_type, exc, tb):
"""Context manager exit."""
LOGGER.debug(
"Exiting IndyVdrLedger context manager\nTraceback:\n%s",
"".join(traceback.format_stack(limit=5)),
)
traceback.print_stack(limit=10)
LOGGER.debug("Exiting IndyVdrLedger context manager")
await self.pool.context_close()
await super().__aexit__(exc_type, exc, tb)

Expand Down

0 comments on commit 5d991da

Please sign in to comment.