mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-06-06 08:09:43 +08:00
refactor(retry): 统一管理 API 最大重试次数配置
将 API 调用的最大重试次数 (`MAX_RETRIES`) 的配置移至 `app.config.config.settings`。 - 修改 `RetryHandler` 以直接从全局设置读取 `MAX_RETRIES`。 - 更新使用 `RetryHandler` 的路由装饰器,移除冗余的 `max_retries` 参数传递。 这使得重试次数的配置更加集中和易于管理。
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
from functools import wraps
|
||||
from typing import Callable, TypeVar
|
||||
|
||||
from app.core.constants import MAX_RETRIES
|
||||
from app.config.config import settings
|
||||
from app.log.logger import get_retry_logger
|
||||
|
||||
T = TypeVar("T")
|
||||
@@ -12,8 +12,7 @@ logger = get_retry_logger()
|
||||
class RetryHandler:
|
||||
"""重试处理装饰器"""
|
||||
|
||||
def __init__(self, max_retries: int = MAX_RETRIES, key_arg: str = "api_key"):
|
||||
self.max_retries = max_retries
|
||||
def __init__(self, key_arg: str = "api_key"):
|
||||
self.key_arg = key_arg
|
||||
|
||||
def __call__(self, func: Callable[..., T]) -> Callable[..., T]:
|
||||
@@ -21,14 +20,14 @@ class RetryHandler:
|
||||
async def wrapper(*args, **kwargs) -> T:
|
||||
last_exception = None
|
||||
|
||||
for attempt in range(self.max_retries):
|
||||
for attempt in range(settings.MAX_RETRIES):
|
||||
retries = attempt + 1
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
except Exception as e:
|
||||
last_exception = e
|
||||
logger.warning(
|
||||
f"API call failed with error: {str(e)}. Attempt {retries} of {self.max_retries}"
|
||||
f"API call failed with error: {str(e)}. Attempt {retries} of {settings.MAX_RETRIES}"
|
||||
)
|
||||
|
||||
# 从函数参数中获取 key_manager
|
||||
|
||||
Reference in New Issue
Block a user