mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
23 lines
626 B
Python
23 lines
626 B
Python
import time
|
|
from typing import Callable, TypeVar
|
|
|
|
from backend.epc_client.exceptions import EpcRateLimitError
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
def call_with_retry(
|
|
fn: Callable[[], T],
|
|
max_retries: int = 5,
|
|
backoff_base: float = 1.0,
|
|
backoff_multiplier: float = 2.0,
|
|
) -> T:
|
|
last_exc: EpcRateLimitError | None = None
|
|
for attempt in range(max_retries + 1):
|
|
try:
|
|
return fn()
|
|
except EpcRateLimitError as exc:
|
|
last_exc = exc
|
|
if attempt < max_retries:
|
|
time.sleep(backoff_base * (backoff_multiplier ** attempt))
|
|
raise last_exc # type: ignore[misc]
|