Skip to content

Commit

Permalink
refactor: move decorators under MultiProvider class
Browse files Browse the repository at this point in the history
  • Loading branch information
madlabman committed May 10, 2024
1 parent 80f7d9e commit 7a451f0
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions src/providers/ipfs/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,6 @@ class MaxRetryError(IPFSError):
...


def retry(fn):
@wraps(fn)
def wrapped(self: SupportsRetries, *args, **kwargs):
retries_left = self.retries
while retries_left:
try:
return fn(self, *args, **kwargs)
except IPFSError as ex:
retries_left -= 1
if not retries_left:
raise MaxRetryError from ex
logger.warning({"msg": f"Retrying a failed call of {fn.__name__}, {retries_left=}", "error": str(ex)})
raise MaxRetryError

return wrapped


def with_fallback(fn):
@wraps(fn)
def wrapped(self: MultiProvider, *args, **kwargs):
try:
result = fn(self, *args, **kwargs)
except IPFSError:
self.current_provider_index = (self.current_provider_index + 1) % len(self.providers)
if self.last_working_provider_index == self.current_provider_index:
logger.error({"msg": "No more IPFS providers left to call"})
raise
return wrapped(self, *args, **kwargs)

self.last_working_provider_index = self.current_provider_index
return result

return wrapped


class MultiIPFSProvider(IPFSProvider, MultiProvider[IPFSProvider]):
"""Fallback-driven provider for IPFS"""

Expand All @@ -82,6 +47,43 @@ def __init__(self, providers: Iterable[IPFSProvider], *, retries: int = 3) -> No
for p in self.providers:
assert isinstance(p, IPFSProvider)

@staticmethod
def with_fallback(fn):
@wraps(fn)
def wrapped(self: MultiProvider, *args, **kwargs):
try:
result = fn(self, *args, **kwargs)
except IPFSError:
self.current_provider_index = (self.current_provider_index + 1) % len(self.providers)
if self.last_working_provider_index == self.current_provider_index:
logger.error({"msg": "No more IPFS providers left to call"})
raise
return wrapped(self, *args, **kwargs)

self.last_working_provider_index = self.current_provider_index
return result

return wrapped

@staticmethod
def retry(fn):
@wraps(fn)
def wrapped(self: SupportsRetries, *args, **kwargs):
retries_left = self.retries
while retries_left:
try:
return fn(self, *args, **kwargs)
except IPFSError as ex:
retries_left -= 1
if not retries_left:
raise MaxRetryError from ex
logger.warning(
{"msg": f"Retrying a failed call of {fn.__name__}, {retries_left=}", "error": str(ex)}
)
raise MaxRetryError

return wrapped

@with_fallback
@retry
def fetch(self, cid: CIDv0 | CIDv1) -> bytes:
Expand Down

0 comments on commit 7a451f0

Please sign in to comment.