hank9999 commited on
Commit ·
1d958fc
1
Parent(s): 3e054c6
feat: 调整重试策略: 单key 3次 单请求最多9次
Browse files- src/kiro/provider.rs +21 -2
src/kiro/provider.rs
CHANGED
|
@@ -14,6 +14,12 @@ use crate::kiro::machine_id;
|
|
| 14 |
use crate::kiro::model::credentials::KiroCredentials;
|
| 15 |
use crate::kiro::token_manager::{CallContext, MultiTokenManager};
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
/// Kiro API Provider
|
| 18 |
///
|
| 19 |
/// 核心组件,负责与 Kiro API 通信
|
|
@@ -144,12 +150,18 @@ impl KiroProvider {
|
|
| 144 |
}
|
| 145 |
|
| 146 |
/// 内部方法:带重试逻辑的 API 调用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
async fn call_api_with_retry(
|
| 148 |
&self,
|
| 149 |
request_body: &str,
|
| 150 |
is_stream: bool,
|
| 151 |
) -> anyhow::Result<reqwest::Response> {
|
| 152 |
-
let
|
|
|
|
| 153 |
let mut last_error: Option<anyhow::Error> = None;
|
| 154 |
|
| 155 |
for attempt in 0..max_retries {
|
|
@@ -238,7 +250,14 @@ impl KiroProvider {
|
|
| 238 |
}
|
| 239 |
|
| 240 |
// 所有重试都失败
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
|
|
|
| 14 |
use crate::kiro::model::credentials::KiroCredentials;
|
| 15 |
use crate::kiro::token_manager::{CallContext, MultiTokenManager};
|
| 16 |
|
| 17 |
+
/// 每个凭据的最大重试次数
|
| 18 |
+
const MAX_RETRIES_PER_CREDENTIAL: usize = 3;
|
| 19 |
+
|
| 20 |
+
/// 总重试次数硬上限(避免无限重试)
|
| 21 |
+
const MAX_TOTAL_RETRIES: usize = 9;
|
| 22 |
+
|
| 23 |
/// Kiro API Provider
|
| 24 |
///
|
| 25 |
/// 核心组件,负责与 Kiro API 通信
|
|
|
|
| 150 |
}
|
| 151 |
|
| 152 |
/// 内部方法:带重试逻辑的 API 调用
|
| 153 |
+
///
|
| 154 |
+
/// 重试策略:
|
| 155 |
+
/// - 每个凭据最多重试 MAX_RETRIES_PER_CREDENTIAL 次
|
| 156 |
+
/// - 总重试次数 = min(凭据数量 × 每凭据重试次数, MAX_TOTAL_RETRIES)
|
| 157 |
+
/// - 硬上限 9 次,避免无限重试
|
| 158 |
async fn call_api_with_retry(
|
| 159 |
&self,
|
| 160 |
request_body: &str,
|
| 161 |
is_stream: bool,
|
| 162 |
) -> anyhow::Result<reqwest::Response> {
|
| 163 |
+
let total_credentials = self.token_manager.total_count();
|
| 164 |
+
let max_retries = (total_credentials * MAX_RETRIES_PER_CREDENTIAL).min(MAX_TOTAL_RETRIES);
|
| 165 |
let mut last_error: Option<anyhow::Error> = None;
|
| 166 |
|
| 167 |
for attempt in 0..max_retries {
|
|
|
|
| 250 |
}
|
| 251 |
|
| 252 |
// 所有重试都失败
|
| 253 |
+
let api_type = if is_stream { "流式" } else { "非流式" };
|
| 254 |
+
Err(last_error.unwrap_or_else(|| {
|
| 255 |
+
anyhow::anyhow!(
|
| 256 |
+
"{} API 请求失败:已达到最大重试次数({}次)",
|
| 257 |
+
api_type,
|
| 258 |
+
max_retries
|
| 259 |
+
)
|
| 260 |
+
}))
|
| 261 |
}
|
| 262 |
}
|
| 263 |
|