retry增加参数适配

This commit is contained in:
HibiKier 2025-02-22 22:53:26 +08:00 committed by GitHub
parent 574c36ed8e
commit d605940f0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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