From d605940f0d382631511248bb0d0825680d4dbbf3 Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Sat, 22 Feb 2025 22:53:26 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20retry=E5=A2=9E=E5=8A=A0=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zhenxun/utils/decorator/retry.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/zhenxun/utils/decorator/retry.py b/zhenxun/utils/decorator/retry.py index 2a6cc757..ddc55584 100644 --- a/zhenxun/utils/decorator/retry.py +++ b/zhenxun/utils/decorator/retry.py @@ -1,16 +1,24 @@ +from anyio import EndOfStream from httpx import ConnectError, HTTPStatusError, TimeoutException from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed class Retry: @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( reraise=True, - stop=stop_after_attempt(3), - wait=wait_fixed(1), - retry=retry_if_exception_type( - (TimeoutException, ConnectError, HTTPStatusError) - ), + stop=stop_after_attempt(retry_count), + wait=wait_fixed(wait), + retry=retry_if_exception_type(base_exceptions), )