You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In testing out with some more logging added to the ClassLoader (#3332), I could see that /status checks would prompt the AskarStorage class to be reloaded each time. I noticed this comes from the DeferLoad class, so added logs as follows:
# Existing code, with extra debug log linesclassDeferLoad:
"""Helper to defer loading of a class definition."""def__init__(self, cls_path: str):
"""Initialize the `DeferLoad` instance with a qualified class path."""self._cls_path=cls_pathself._inst=Nonedef__call__(self, *args, **kwargs):
"""Magic method to call the `DeferLoad` as a function."""LOGGER.debug("Calling deferred class load for: %s", self._cls_path)
return (self.resolved)(*args, **kwargs)
@propertydefresolved(self):
"""Accessor for the resolved class instance."""LOGGER.debug("Resolving deferred class load for: %s", self._cls_path)
ifnotself._inst:
LOGGER.debug("Class not yet loaded, loading: %s", self._cls_path)
self._inst=ClassLoader.load_class(self._cls_path)
LOGGER.debug("Successfully loaded class: %s", self._cls_path)
returnself._inst
Results in logs as follows:
acapy_agent.admin.server DEBUG Incoming request: GET /status/live
acapy_agent.admin.server DEBUG Match info: <MatchInfo {}: <ResourceRoute [GET] <PlainResource /status/live> -> <function liveliness_handler at 0x7f2b83f17a60>>
acapy_agent.admin.server DEBUG Body: None
acapy_agent.admin.server DEBUG Incoming request: GET /status/ready
acapy_agent.admin.server DEBUG Match info: <MatchInfo {}: <ResourceRoute [GET] <PlainResource /status/ready> -> <function readiness_handler at 0x7f2b83f17ba0>>
acapy_agent.admin.server DEBUG Body: None
acapy_agent.utils.classloader DEBUG Calling deferred class load for: acapy_agent.storage.askar.AskarStorage
acapy_agent.utils.classloader DEBUG Resolving deferred class load for: acapy_agent.storage.askar.AskarStorage
acapy_agent.utils.classloader DEBUG Class not yet loaded, loading: acapy_agent.storage.askar.AskarStorage
acapy_agent.utils.classloader DEBUG Successfully loaded class: acapy_agent.storage.askar.AskarStorage
acapy_agent.utils.classloader DEBUG Calling deferred class load for: acapy_agent.storage.askar.AskarStorage
acapy_agent.utils.classloader DEBUG Resolving deferred class load for: acapy_agent.storage.askar.AskarStorage
acapy_agent.utils.classloader DEBUG Class not yet loaded, loading: acapy_agent.storage.askar.AskarStorage
acapy_agent.utils.classloader DEBUG Successfully loaded class: acapy_agent.storage.askar.AskarStorage
and repeats upon each status check.
This indicates that the self._inst = ClassLoader.load_class(self._cls_path) line does not persist the class for future re-use, and the load_class logic is executed each time.
This is presumably because a new instance of the DeferLoad class is created each time it is called. I can't be completely sure, but I guess it's that when a DeferLoad instance is called, a new one is being instantiated each time, and not being re-used. So the persistence to self._inst doesn't work as intended.
I found that adding class caching as follows is sufficient to solve this:
classDeferLoad:
"""Helper to defer loading of a class definition."""_class_cache= {} # Shared cache for resolved classesdef__init__(self, cls_path: str):
"""Initialize the `DeferLoad` instance with a qualified class path."""self._cls_path=cls_pathdef__call__(self, *args, **kwargs):
"""Magic method to call the `DeferLoad` as a function."""LOGGER.debug("Calling deferred class load for: %s", self._cls_path)
returnself.resolved(*args, **kwargs)
@propertydefresolved(self):
"""Accessor for the resolved class instance."""LOGGER.debug("Resolving deferred class load for: %s", self._cls_path)
ifself._cls_pathnotinDeferLoad._class_cache:
LOGGER.debug("Class not yet loaded, loading: %s", self._cls_path)
DeferLoad._class_cache[self._cls_path] =ClassLoader.load_class(
self._cls_path
)
LOGGER.debug("Successfully loaded class: %s", self._cls_path)
returnDeferLoad._class_cache[self._cls_path]
The above is applied in #3361 (without the debug logs)
The text was updated successfully, but these errors were encountered:
ff137
added a commit
to didx-xyz/acapy
that referenced
this issue
Nov 28, 2024
In testing out with some more logging added to the ClassLoader (#3332), I could see that /status checks would prompt the
AskarStorage
class to be reloaded each time. I noticed this comes from the DeferLoad class, so added logs as follows:Results in logs as follows:
and repeats upon each status check.
This indicates that the
self._inst = ClassLoader.load_class(self._cls_path)
line does not persist the class for future re-use, and theload_class
logic is executed each time.This is presumably because a new instance of the DeferLoad class is created each time it is called. I can't be completely sure, but I guess it's that when a DeferLoad instance is called, a new one is being instantiated each time, and not being re-used. So the persistence to
self._inst
doesn't work as intended.I found that adding class caching as follows is sufficient to solve this:
The above is applied in #3361 (without the debug logs)
The text was updated successfully, but these errors were encountered: