Files
zhenxun_bot/zhenxun/utils/decorator/retry.py
T

25 lines
730 B
Python
Raw Normal View History

2025-02-24 09:28:53 +08:00
from anyio import EndOfStream
2025-02-03 21:23:14 +08:00
from httpx import ConnectError, HTTPStatusError, TimeoutException
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed
class Retry:
@staticmethod
2025-02-24 09:28:53 +08:00
def api(
retry_count: int = 3, wait: int = 1, exception: tuple[type[Exception], ...] = ()
):
2025-02-03 21:23:14 +08:00
"""接口调用重试"""
2025-02-24 09:28:53 +08:00
base_exceptions = (
TimeoutException,
ConnectError,
HTTPStatusError,
EndOfStream,
*exception,
)
2025-02-03 21:23:14 +08:00
return retry(
reraise=True,
2025-02-24 09:28:53 +08:00
stop=stop_after_attempt(retry_count),
wait=wait_fixed(wait),
retry=retry_if_exception_type(base_exceptions),
2025-02-03 21:23:14 +08:00
)