fix: support nested attributes in client retry wrapper

This commit is contained in:
gmega 2025-01-14 19:48:58 -03:00
parent 742578a3f7
commit 1ee8ea8a35
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
2 changed files with 30 additions and 8 deletions

View File

@ -13,7 +13,7 @@ from tenacity import retry, wait_exponential, stop_after_attempt
from tenacity.stop import stop_base from tenacity.stop import stop_base
from tenacity.wait import wait_base from tenacity.wait import wait_base
from torrentool.torrent import Torrent from torrentool.torrent import Torrent
from typing_extensions import Generic, TypeVar from typing_extensions import TypeVar
from urllib3.util import Url from urllib3.util import Url
from benchmarks.core.experiments.experiments import ExperimentComponent from benchmarks.core.experiments.experiments import ExperimentComponent
@ -171,18 +171,25 @@ class DelugeNode(SharedFSNode[Torrent, DelugeMeta], ExperimentComponent):
T = TypeVar("T") T = TypeVar("T")
class ResilientCallWrapper(Generic[T]): class ResilientCallWrapper:
def __init__(self, client: T, wait_policy: wait_base, stop_policy: stop_base): def __init__(self, node: Any, wait_policy: wait_base, stop_policy: stop_base):
self.client = client self.node = node
self.wait_policy = wait_policy self.wait_policy = wait_policy
self.stop_policy = stop_policy self.stop_policy = stop_policy
def __getattr__(self, item): def __call__(self, *args, **kwargs):
@retry(wait=self.wait_policy, stop=self.stop_policy) @retry(wait=self.wait_policy, stop=self.stop_policy)
def _resilient_wrapper(*args, **kwargs): def _resilient_wrapper():
return getattr(self.client, item)(*args, **kwargs) return self.node(*args, **kwargs)
return _resilient_wrapper return _resilient_wrapper()
def __getattr__(self, item):
return ResilientCallWrapper(
getattr(self.node, item),
wait_policy=self.wait_policy,
stop_policy=self.stop_policy,
)
class DelugeDownloadHandle(DownloadHandle): class DelugeDownloadHandle(DownloadHandle):

View File

@ -106,3 +106,18 @@ def test_should_give_up_on_operations_when_stop_policy_is_met():
with pytest.raises(RetryError): with pytest.raises(RetryError):
wrapper.flaky() wrapper.flaky()
class NestedFlaky:
def __init__(self):
self.client = FlakyClient()
def test_should_resolve_nested_objects():
wrapper = ResilientCallWrapper(
NestedFlaky(),
wait_policy=wait_incrementing(start=0, increment=0),
stop_policy=stop_after_attempt(2),
)
assert wrapper.client.flaky() == 1