feat(unreachable-cooldown): 连接层不可达客户端级冷却,四家判定下沉共用

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 绿。
This commit is contained in:
2026-10-23 20:58:32 +08:00
parent 6af19b51ba
commit 09dfb01f9c
22 changed files with 426 additions and 99 deletions
@@ -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();
}
}