From 09dfb01f9c593f5e73255175414dd813f7d7f577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Fri, 23 Oct 2026 20:58:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(unreachable-cooldown):=20=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=B1=82=E4=B8=8D=E5=8F=AF=E8=BE=BE=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E7=BA=A7=E5=86=B7=E5=8D=B4=EF=BC=8C=E5=9B=9B=E5=AE=B6?= =?UTF-8?q?=E5=88=A4=E5=AE=9A=E4=B8=8B=E6=B2=89=E5=85=B1=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BACKLOG B24(窗长 300s 已拍板):连续 2 次连接未建立即对该 host 进 冷却窗,窗内调用零探针快速失败(归因文案与调用内短路逐字一致);到期 后下一条真实调用兼作探针,失败重进、成功恢复。isConnectFailure 四处 副本(BrandCheckClient/AppearancePatentLlmClient/SimilarAsinLlmClient/ TaskOwnerForwardService)下沉 common/retry 共用。三家客户端接入同一 冷却表(两个 LLM 客户端共用 ai.t8star.org 一格,16890 独立一格), 窗长可配(0=关闭退回调用内短路)。另修 CollectDataBrandBatchFilter 首批预算竞态(1ms 预算下 GC 停顿误切首批,照 BrandCheckClient 首轮 不掐契约补 start>0 守卫)。变异验证:冷却禁用 6 例判红;定向 47/47 绿。 --- .../common/retry/UnreachableCooldown.java | 103 ++++++++++++++++++ .../service/TaskOwnerForwardService.java | 14 +-- .../config/AppearancePatentProperties.java | 7 ++ .../aiimage/config/BrandCheckProperties.java | 7 ++ .../aiimage/config/SimilarAsinProperties.java | 7 ++ .../client/AppearancePatentLlmClient.java | 39 ++++--- .../brand/client/BrandCheckClient.java | 41 +++++-- .../util/CollectDataBrandBatchFilter.java | 5 +- .../client/SimilarAsinLlmClient.java | 46 ++++---- .../src/main/resources/application.yml | 9 ++ .../common/retry/UnreachableCooldownTest.java | 77 +++++++++++++ .../config/HttpClientConnectionReuseTest.java | 15 +-- .../ExternalCallMetricsRecorderTest.java | 19 ++-- .../AppearancePatentBrandBatchTest.java | 5 +- .../AppearancePatentLlmClientHttpTest.java | 7 +- ...earancePatentLlmClientImageDedupeTest.java | 5 +- .../client/AppearancePatentLlmClientTest.java | 15 +-- ...pearancePatentLlmClientTokenUsageTest.java | 5 +- .../brand/client/BrandCheckClientTest.java | 43 +++++++- .../BrandCheckClientTitleBatchTest.java | 3 +- .../client/SimilarAsinLlmClientHttpTest.java | 50 ++++++++- .../service/SimilarAsinLlmLocalVerify.java | 3 +- 22 files changed, 426 insertions(+), 99 deletions(-) create mode 100644 backend-java/src/main/java/com/nanri/aiimage/common/retry/UnreachableCooldown.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/common/retry/UnreachableCooldownTest.java diff --git a/backend-java/src/main/java/com/nanri/aiimage/common/retry/UnreachableCooldown.java b/backend-java/src/main/java/com/nanri/aiimage/common/retry/UnreachableCooldown.java new file mode 100644 index 000000000..c91bdfe6f --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/common/retry/UnreachableCooldown.java @@ -0,0 +1,103 @@ +package com.nanri.aiimage.common.retry; + +import org.springframework.stereotype.Component; + +import java.net.ConnectException; +import java.net.NoRouteToHostException; +import java.net.UnknownHostException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +/** + * 连接层不可达的客户端级冷却表(按上游 host 分键,Spring 单例)。 + * + *
背景(BACKLOG B24,2026-10-23 拍板):三家同型客户端({@code BrandCheckClient}/ + * {@code AppearancePatentLlmClient}/{@code SimilarAsinLlmClient})的连接层短路计数 + * 生命周期都只到单次调用,网关长断时每个新调用/分块仍要各自真发连接探针才知道"还是断的"。 + * 本表把「连续 2 次连接未建立」的证据跨调用累计,达到阈值即对该 host 进入冷却窗 + * (默认 300s,同仓两处先例:{@code pt_worker/proxy_pool.py} 与 16890 服务侧的钥匙冷却), + * 窗内调用方不发请求直接快速失败;到期后下一条真实调用兼作探针,失败即重进、成功即恢复。 + * + *
调用方契约:{@link #isCoolingDown} 在发请求前问一次;连接层失败记
+ * {@link #recordConnectFailure}(返回 true 的那方打一条进入冷却的 warn);
+ * 连接成功或"连上了没答好"(读超时/5xx/解析失败)记 {@link #recordConnected} 清零证据。
+ * 窗长 {@code <= 0} 表示关闭冷却,全部方法退化为无操作——调用方退回各自的调用内短路行为。
+ */
+@Component
+public class UnreachableCooldown {
+
+ /**
+ * 进入冷却的证据数:与三家客户端各自的调用内短路阈值同数字——一次 refused 可能是抖动,
+ * 两次同形状且中间没有任何一次成功,才是服务在掉。
+ */
+ private static final int EVIDENCE_THRESHOLD = 2;
+
+ private final ConcurrentHashMap 故意不含读超时与 5xx:那两种是"连上了但这次没答好",重试救得回来;把它们算进
* 不可达证据,就会把一次上游抖动误判成服务在掉、把同批其余品牌的重试一起掐掉。
*/
- static boolean isConnectFailure(Throwable error) {
- for (Throwable t = error; t != null && t != t.getCause(); t = t.getCause()) {
- if (t instanceof ConnectException || t instanceof NoRouteToHostException
- || t instanceof UnknownHostException) {
- return true;
- }
+ /** 连接层失败记入客户端级冷却表:达到阈值的那方打一条进入冷却的 warn(B24)。 */
+ private void recordConnectFailure(Exception ex) {
+ long cooldownMillis = Math.max(0, properties.getUnreachableCooldownMillis());
+ if (unreachableCooldown.recordConnectFailure(properties.getBaseUrl(), cooldownMillis)) {
+ log.warn("[brand-check] SERVICE_UNREACHABLE 连接层证据累计 {} 次,进入客户端级冷却 {}ms host={}",
+ CONNECT_FAILURE_EVIDENCE, cooldownMillis, properties.getBaseUrl());
}
- return false;
+ }
+
+ /**
+ * 连接层判定下沉 {@code UnreachableCooldown.isConnectFailure}(B24):判定口径与
+ * 两个 LLM 客户端共用一份,不再各留包内副本。
+ */
+ static boolean isConnectFailure(Throwable error) {
+ return com.nanri.aiimage.common.retry.UnreachableCooldown.isConnectFailure(error);
}
/**
diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilter.java b/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilter.java
index b564b089e..f15976862 100644
--- a/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilter.java
+++ b/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilter.java
@@ -118,7 +118,10 @@ public class CollectDataBrandBatchFilter {
}
continue;
}
- if (deadlineNanos != Long.MAX_VALUE && System.nanoTime() >= deadlineNanos) {
+ // 首批不掐(与 BrandCheckClient 的 attempt>1 同契约):预算检查只拦后续批次。
+ // 对首批也查的话,deadline 计算与检查之间一次 GC/调度停顿(毫秒级)就会把首批
+ // 整组误切成 queryFailed——1ms 预算的测试已实测抖出过这个形状。
+ if (start > 0 && deadlineNanos != Long.MAX_VALUE && System.nanoTime() >= deadlineNanos) {
// 预算用尽:不再为后面的批次捶上游。已缓存的品牌仍按缓存结论归类(不花代价),
// 没结论的行归 queryFailed 入补测队列——既不按 accepted 放行(可能正是侵权品牌),
// 也不按 rejected 拉黑(没查过就不该写进无效品牌表,否则永久误杀)。
diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/client/SimilarAsinLlmClient.java b/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/client/SimilarAsinLlmClient.java
index 9ed39be45..56981bb99 100644
--- a/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/client/SimilarAsinLlmClient.java
+++ b/backend-java/src/main/java/com/nanri/aiimage/modules/similarasin/client/SimilarAsinLlmClient.java
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.similarasin.client;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.retry.LlmRetryBackoff;
+import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.HttpClientPool;
import com.nanri.aiimage.config.SimilarAsinProperties;
import com.nanri.aiimage.metrics.ExternalCallMetricsRecorder;
@@ -17,9 +18,6 @@ import org.springframework.web.client.RestClient;
import java.io.IOException;
import java.io.InputStream;
-import java.net.ConnectException;
-import java.net.NoRouteToHostException;
-import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
@@ -52,6 +50,7 @@ public class SimilarAsinLlmClient {
private final ObjectMapper objectMapper;
private final ExternalCallMetricsRecorder externalCallMetrics;
private final UserSecretUsageService userSecretUsageService;
+ private final UnreachableCooldown unreachableCooldown;
private volatile RestClient sharedRestClient;
/** 首次尝试专用客户端(更短读超时,快速失败);配置未生效时与 sharedRestClient 相同。 */
@@ -60,11 +59,13 @@ public class SimilarAsinLlmClient {
public SimilarAsinLlmClient(SimilarAsinProperties properties,
ObjectMapper objectMapper,
ExternalCallMetricsRecorder externalCallMetrics,
- UserSecretUsageService userSecretUsageService) {
+ UserSecretUsageService userSecretUsageService,
+ UnreachableCooldown unreachableCooldown) {
this.properties = properties;
this.objectMapper = objectMapper;
this.externalCallMetrics = externalCallMetrics;
this.userSecretUsageService = userSecretUsageService;
+ this.unreachableCooldown = unreachableCooldown;
}
/** 文本对话,json_object 输出。 */
@@ -85,16 +86,32 @@ public class SimilarAsinLlmClient {
String responseFormat) {
String resolvedKey = resolveApiKey(apiKey);
int attempts = Math.max(1, properties.getLlmRetryTimes());
+ long cooldownMillis = Math.max(0, properties.getLlmUnreachableCooldownMillis());
+ String host = properties.getLlmHost();
+ if (unreachableCooldown.isCoolingDown(host, cooldownMillis)) {
+ // 冷却期快失败不发请求、不记密钥次数:结论与短路版一致(调用方拿不到 LLM 结论),
+ // 差别只是不再为"确认还是断的"各烧一轮连接探针(B24)
+ log.debug("[similar-asin][llm] 客户端级冷却中,快速失败 model={} window={}ms", model, cooldownMillis);
+ throw new IllegalStateException(REASON_LLM_UNREACHABLE + ";lastErr=冷却中(连续连接未建立,暂不打上游)");
+ }
Exception lastFailure = null;
int connectFailures = 0;
for (int attempt = 1; attempt <= attempts; attempt++) {
try {
- return invokeChatOnce(model, system, userText, images, resolvedKey, responseFormat, attempt);
+ String content = invokeChatOnce(model, system, userText, images, resolvedKey, responseFormat, attempt);
+ unreachableCooldown.recordConnected(host);
+ return content;
} catch (Exception ex) {
lastFailure = ex;
- if (isConnectFailure(ex)) {
+ if (UnreachableCooldown.isConnectFailure(ex)) {
connectFailures++;
- if (connectFailures >= LLM_CONNECT_FAILURE_EVIDENCE) {
+ boolean entered = unreachableCooldown.recordConnectFailure(host, cooldownMillis);
+ if (entered) {
+ log.warn("[similar-asin][llm] 连接层证据累计 {} 次,进入客户端级冷却 {}ms host={}",
+ 2, cooldownMillis, host);
+ }
+ if (connectFailures >= LLM_CONNECT_FAILURE_EVIDENCE
+ || unreachableCooldown.isCoolingDown(host, cooldownMillis)) {
log.warn("[similar-asin][llm] LLM_UNREACHABLE 连续 {} 次连接未建立,判定 LLM 服务不可达,"
+ "停止重试 attempt={} model={} err={}",
LLM_CONNECT_FAILURE_EVIDENCE, attempt, model, failureMessage(ex));
@@ -106,6 +123,7 @@ public class SimilarAsinLlmClient {
} else {
// 连上了只是这次没答好(读超时/解析失败/5xx):连接层证据不作数
connectFailures = 0;
+ unreachableCooldown.recordConnected(host);
}
if (attempt >= attempts) {
break;
@@ -123,20 +141,6 @@ public class SimilarAsinLlmClient {
: new IllegalStateException(lastFailure.getMessage(), lastFailure);
}
- /**
- * 只认连接层故障(顺着 cause 链找):连接被拒/无路由/主机名解不了。
- * 读超时与 5xx 是"连上了没答好",故意不算——那类抖动重试是有意义的。
- */
- static boolean isConnectFailure(Throwable error) {
- for (Throwable t = error; t != null && t != t.getCause(); t = t.getCause()) {
- if (t instanceof ConnectException || t instanceof NoRouteToHostException
- || t instanceof UnknownHostException) {
- return true;
- }
- }
- return false;
- }
-
/** 只认任务/用户自己的 api_key(payload → 按 uid 的服务端密钥表已在 readApiKey 兜过);
* 为空即空、跳过 LLM,绝不回落平台公费密钥 —— 各用户的任务只能烧各自的密钥。 */
public String resolveApiKey(String apiKey) {
diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml
index a02d0144d..d2f592039 100644
--- a/backend-java/src/main/resources/application.yml
+++ b/backend-java/src/main/resources/application.yml
@@ -378,6 +378,9 @@ aiimage:
total-timeout-millis: ${AIIMAGE_BRAND_CHECK_TOTAL_TIMEOUT_MILLIS:90000}
connect-timeout-millis: ${AIIMAGE_BRAND_CHECK_CONNECT_TIMEOUT_MILLIS:10000}
read-timeout-millis: ${AIIMAGE_BRAND_CHECK_READ_TIMEOUT_MILLIS:60000}
+ # 连接层不可达的客户端级冷却窗长(B24,0=关闭):连续 2 次连接未建立即进窗,
+ # 窗内整批品牌不发请求直接按服务不可达判失败;到期后下一批的真实请求兼作探针。
+ unreachable-cooldown-millis: ${AIIMAGE_BRAND_CHECK_UNREACHABLE_COOLDOWN_MILLIS:300000}
appearance-patent:
no-result-upload-timeout-minutes: ${AIIMAGE_APPEARANCE_PATENT_NO_RESULT_UPLOAD_TIMEOUT_MINUTES:180}
llm-host: ${AIIMAGE_APPEARANCE_PATENT_LLM_HOST:https://ai.t8star.org}
@@ -402,6 +405,9 @@ aiimage:
# 实测空行 20.7%~51.3%,全部放行且零告警,因为旧分母按解析行标题判、用户表不带标题列时分母为 0)。
llm-coverage-min-ratio: ${AIIMAGE_APPEARANCE_PATENT_LLM_COVERAGE_MIN_RATIO:0.95}
llm-retry-times: ${AIIMAGE_APPEARANCE_PATENT_LLM_RETRY_TIMES:3}
+ # 连接层不可达的客户端级冷却窗长(B24,0=关闭):连续 2 次连接未建立即进窗,
+ # 窗内调用不发请求直接按服务不可达快速失败;到期后下一条真实调用兼作探针。
+ llm-unreachable-cooldown-millis: ${AIIMAGE_APPEARANCE_PATENT_LLM_UNREACHABLE_COOLDOWN_MILLIS:300000}
# 单行最多被重发几轮(<=0 关闭)。失败行会被释放、下一轮重新申领,没有这道闸时
# 上游劣化窗口内会无限重发并每轮扣用户 LLM 次数——deferRunning 不累加 retryCount,任务层那道永不触发。
llm-row-max-rounds: ${AIIMAGE_APPEARANCE_PATENT_LLM_ROW_MAX_ROUNDS:3}
@@ -440,6 +446,9 @@ aiimage:
llm-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_READ_TIMEOUT_MILLIS:600000}
llm-first-attempt-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_FIRST_ATTEMPT_READ_TIMEOUT_MILLIS:300000}
llm-retry-times: ${AIIMAGE_SIMILAR_ASIN_LLM_RETRY_TIMES:3}
+ # 连接层不可达的客户端级冷却窗长(B24,0=关闭):连续 2 次连接未建立即进窗,
+ # 窗内调用不发请求直接按服务不可达快速失败;到期后下一条真实调用兼作探针。
+ llm-unreachable-cooldown-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_UNREACHABLE_COOLDOWN_MILLIS:300000}
llm-row-concurrency: ${AIIMAGE_SIMILAR_ASIN_LLM_ROW_CONCURRENCY:5}
llm-category-batch-size: ${AIIMAGE_SIMILAR_ASIN_LLM_CATEGORY_BATCH_SIZE:10}
# 合规判定一次请求合并的行数:省往返不省判定,每件商品仍各自出 is_conform/reason/category。
diff --git a/backend-java/src/test/java/com/nanri/aiimage/common/retry/UnreachableCooldownTest.java b/backend-java/src/test/java/com/nanri/aiimage/common/retry/UnreachableCooldownTest.java
new file mode 100644
index 000000000..272014e9e
--- /dev/null
+++ b/backend-java/src/test/java/com/nanri/aiimage/common/retry/UnreachableCooldownTest.java
@@ -0,0 +1,77 @@
+package com.nanri.aiimage.common.retry;
+
+import org.junit.jupiter.api.Test;
+
+import java.net.ConnectException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * 连接层不可达冷却表的口径(B24):阈值 2、证据可清零、窗长到点自然到期、
+ * 到期后一次失败即重进、窗长 0 整体关闭、同 host 不同写法必须同格。
+ */
+class UnreachableCooldownTest {
+
+ private final UnreachableCooldown cooldown = new UnreachableCooldown();
+ private static final long WINDOW = 60_000L;
+
+ @Test
+ void twoConsecutiveFailuresEnterCooldownOneDoesNot() {
+ assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
+ assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
+ assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
+ // 第二次失败:冷却窗从无到有,返回 true 的那方打进入 warn;之后的失败不重复报
+ assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isTrue();
+ assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isTrue();
+ assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
+ }
+
+ @Test
+ void connectedResetsEvidenceSoTwoFreshFailuresAreRequiredAgain() {
+ cooldown.recordConnectFailure("https://gw.example.com", WINDOW);
+ cooldown.recordConnected("https://gw.example.com");
+ assertThat(cooldown.recordConnectFailure("https://gw.example.com", WINDOW)).isFalse();
+ assertThat(cooldown.isCoolingDown("https://gw.example.com", WINDOW)).isFalse();
+ }
+
+ @Test
+ void windowExpiresNaturallyAndSingleFailureReenters() throws InterruptedException {
+ String host = "https://short.example.com";
+ cooldown.recordConnectFailure(host, 50);
+ cooldown.recordConnectFailure(host, 50);
+ assertThat(cooldown.isCoolingDown(host, 50)).isTrue();
+
+ Thread.sleep(120);
+ assertThat(cooldown.isCoolingDown(host, 50)).isFalse();
+
+ // 历史证据不清零:到期后探针再失败,一次即重进(B24 拍板口径)
+ assertThat(cooldown.recordConnectFailure(host, 50)).isTrue();
+ assertThat(cooldown.isCoolingDown(host, 50)).isTrue();
+ }
+
+ @Test
+ void zeroWindowDisablesCooldownEntirely() {
+ for (int i = 0; i < 5; i++) {
+ assertThat(cooldown.recordConnectFailure("https://gw.example.com", 0)).isFalse();
+ }
+ assertThat(cooldown.isCoolingDown("https://gw.example.com", 0)).isFalse();
+ }
+
+ @Test
+ void sameHostDifferentSpellingSharesOneCell() {
+ cooldown.recordConnectFailure("https://GW.example.com", WINDOW);
+ // 尾斜杠/大小写不同写法落进同一格:这条是第 2 次失败,必须进冷却
+ assertThat(cooldown.recordConnectFailure("https://gw.example.com/", WINDOW)).isTrue();
+ assertThat(cooldown.isCoolingDown("https://Gw.Example.Com", WINDOW)).isTrue();
+ }
+
+ @Test
+ void isConnectFailureWalksCauseChainAndIgnoresOtherFailures() {
+ assertThat(UnreachableCooldown.isConnectFailure(new ConnectException("refused"))).isTrue();
+ RuntimeException wrapped = new RuntimeException(
+ new IllegalStateException(new ConnectException("refused")));
+ assertThat(UnreachableCooldown.isConnectFailure(wrapped)).isTrue();
+ assertThat(UnreachableCooldown.isConnectFailure(new RuntimeException("read timeout"))).isFalse();
+ assertThat(UnreachableCooldown.isConnectFailure(null)).isFalse();
+ }
+}
diff --git a/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java b/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java
index 37969d8ca..a099c5cf7 100644
--- a/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java
+++ b/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java
@@ -1,6 +1,7 @@
package com.nanri.aiimage.config;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient.BrandCheckBatchResult;
import com.nanri.aiimage.modules.similarasin.client.SimilarAsinLlmClient;
@@ -96,7 +97,7 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_default_path() throws Exception {
// 默认路径:品牌检查客户端通过共享池创建单例 RestClient,
// 工厂为带 keep-alive 连接池的 JdkClientHttpRequestFactory。
- BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
+ BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
BrandCheckBatchResult result = client.checkTitleText(" ", null, null);
assertTrue(result.brands().isEmpty(), "空标题安全跳过,不创建无效资源");
@@ -107,8 +108,8 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_multiple_items() throws Exception {
// 批量场景:LLM/品牌/紫鸟三个客户端各自持有独立 RestClient,
// 但底层连接池共用同一 HttpClient 实例,不重复创建。
- SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null);
- BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
+ SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null, new UnreachableCooldown());
+ BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
HttpClient llmClient = clientOf(factoryOf(restClientOf(llm)));
@@ -124,7 +125,7 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_repeated_operation_is_idempotent() throws Exception {
// 幂等:同一客户端重复触发请求创建逻辑只产生一个 RestClient,
// 重复调用返回同一实例,不重复创建客户端对象。
- BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
+ BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
assertSame(restClientOf(brand), restClientOf(brand), "品牌客户端复用同一 RestClient");
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
@@ -134,7 +135,7 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_empty_input() throws Exception {
// 空输入:空品牌列表不发起任何 HTTP 请求、不创建客户端资源。
- BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
+ BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms", null);
assertTrue(result.brands().isEmpty());
@@ -145,7 +146,7 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_single_item() throws Exception {
// 单元素:单客户端单请求走共享池,工厂带连接池,行为与批量一致。
- BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
+ BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown());
assertPooled(factoryOf(restClientOf(brand)));
}
@@ -155,7 +156,7 @@ class HttpClientConnectionReuseTest {
// 不随实例数量线性增长连接资源。
int instances = 8;
for (int i = 0; i < instances; i++) {
- restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null));
+ restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null, new UnreachableCooldown()));
restClientOf(new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()));
}
assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(),
diff --git a/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java b/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java
index f51e37c33..4cf508812 100644
--- a/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java
+++ b/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java
@@ -1,6 +1,7 @@
package com.nanri.aiimage.metrics;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.config.SimilarAsinProperties;
import com.nanri.aiimage.config.ZiniaoProperties;
@@ -162,7 +163,7 @@ class ExternalCallMetricsRecorderTest {
SimilarAsinProperties props = llmProps();
SimilarAsinLlmClient client =
new SimilarAsinLlmClient(props, objectMapper, new ExternalCallMetricsRecorder(registry),
- mock(UserSecretUsageService.class));
+ mock(UserSecretUsageService.class), new UnreachableCooldown());
String content = client.invokeChat("test-model", "system", "hello", "test-key");
@@ -181,7 +182,7 @@ class ExternalCallMetricsRecorderTest {
SimilarAsinProperties props = llmProps();
SimilarAsinLlmClient client =
new SimilarAsinLlmClient(props, objectMapper, new ExternalCallMetricsRecorder(registry),
- mock(UserSecretUsageService.class));
+ mock(UserSecretUsageService.class), new UnreachableCooldown());
for (int i = 0; i < 3; i++) {
client.invokeChat("test-model", "system", "prompt-" + i, "test-key");
@@ -192,7 +193,7 @@ class ExternalCallMetricsRecorderTest {
"client", "llm", "result", "success"), "三次成功全部记录");
// 全部外部客户端(LLM / 品牌检查 / 紫鸟)在同一次批量中各自记录指标
- BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
+ BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
brand.checkAll(List.of("Nintendo", "LEGO", "Sony"), "Terms", 7L);
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(ziniaoProps(), objectMapper, new ExternalCallMetricsRecorder(registry));
assertEquals(1001L, ziniao.getCompanyIdByApiKey("test-api-key"));
@@ -210,7 +211,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_normal_repeated_operation_is_idempotent() throws Exception {
BrandCheckProperties props = brandProps();
- BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
+ BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
client.check("Nintendo", "Terms", 7L);
client.check("Nintendo", "Terms", 7L);
@@ -227,7 +228,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_empty_input() throws Exception {
BrandCheckProperties props = brandProps();
- BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
+ BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of(), "Terms", 7L);
@@ -242,7 +243,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_single_item() throws Exception {
BrandCheckProperties props = brandProps();
- BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
+ BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
BrandCheckClient.BrandCheckResponse response = client.check("LEGO", "Terms", 7L);
@@ -264,7 +265,7 @@ class ExternalCallMetricsRecorderTest {
int index = i;
pool.submit(() -> {
SimilarAsinLlmClient client = new SimilarAsinLlmClient(props, objectMapper, recorder,
- mock(UserSecretUsageService.class));
+ mock(UserSecretUsageService.class), new UnreachableCooldown());
try {
client.invokeChat("test-model", "system", "prompt-" + index, "test-key");
} catch (Exception ignored) {
@@ -297,7 +298,7 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_invalid_input_rejected() throws Exception {
BrandCheckProperties props = brandProps();
- BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
+ BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets(), new UnreachableCooldown());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(null, "Terms", 7L);
@@ -315,7 +316,7 @@ class ExternalCallMetricsRecorderTest {
SimilarAsinProperties props = llmProps();
SimilarAsinLlmClient client =
new SimilarAsinLlmClient(props, objectMapper, recorder,
- mock(UserSecretUsageService.class));
+ mock(UserSecretUsageService.class), new UnreachableCooldown());
// 第一次调用走 500 失败路径,第二次调用恢复成功:错误可恢复
llmFailNext.set(true);
diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java
index c2af37bb9..794d4b12e 100644
--- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java
+++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
@@ -46,7 +47,7 @@ class AppearancePatentBrandBatchTest {
null,
brandCheckClient,
mock(UserSecretUsageService.class)
- );
+ , new UnreachableCooldown());
@Test
void mergedBatchAsksBrandCheckOnceAndAttributesPerRow() throws Exception {
@@ -115,7 +116,7 @@ class AppearancePatentBrandBatchTest {
properties.setLlmRetryTimes(1);
properties.setLlmReadTimeoutMillis(5_000);
AppearancePatentLlmClient inspectClient = new AppearancePatentLlmClient(
- properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class));
+ properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class), new UnreachableCooldown());
List