feat(cost-guard): 代理/LLM 损耗监控与账号级任务暂停

- 每分钟扫描用量增量/日累计,异常置暂停闸并推站内通知
- 品牌补测、跟价派发、外观激活、品牌检测与两个 LLM 客户端统一接闸
- 同一代理/LLM 密钥被 3 个以上账号共用时给出用户提示
- 品牌检测出口拒绝批级熔断;LLM 同 key 共享并发/间隔/429 惩罚
- V170 新增 biz_user_task_guard
This commit is contained in:
2026-10-24 10:50:20 +08:00
parent 052f2bd4a5
commit 9c4905c94e
32 changed files with 1625 additions and 5 deletions
@@ -2,6 +2,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.LlmKeyGate;
import com.nanri.aiimage.common.retry.LlmRetryBackoff;
import com.nanri.aiimage.common.retry.UnreachableCooldown;
import com.nanri.aiimage.config.HttpClientPool;
@@ -50,6 +51,10 @@ public class SimilarAsinLlmClient {
private final ObjectMapper objectMapper;
private final ExternalCallMetricsRecorder externalCallMetrics;
private final UserSecretUsageService userSecretUsageService;
/** 损耗闸:字段注入避免改动手工构造的 LLM 用例;null=未启用(单测)。 */
@org.springframework.beans.factory.annotation.Autowired(required = false)
private com.nanri.aiimage.modules.usersecret.service.UserTaskGuardService taskGuard;
private final UnreachableCooldown unreachableCooldown;
private volatile RestClient sharedRestClient;
@@ -97,11 +102,23 @@ public class SimilarAsinLlmClient {
Exception lastFailure = null;
int connectFailures = 0;
for (int attempt = 1; attempt <= attempts; attempt++) {
try {
try (LlmKeyGate.Permit ignored = LlmKeyGate.acquire(resolvedKey)) {
String content = invokeChatOnce(model, system, userText, images, resolvedKey, responseFormat, attempt);
unreachableCooldown.recordConnected(host);
return content;
} catch (Exception ex) {
if (ex instanceof com.nanri.aiimage.common.exception.TaskPausedException paused) {
// 暂停是账号级结论,重试不会变好;已完成的行走正常落库
throw paused;
}
if (ex instanceof LlmKeyGate.QueueTimeoutException) {
// 本地 key 容量已满:重试只会继续排队,直接交给行级失败
throw (LlmKeyGate.QueueTimeoutException) ex;
}
if (LlmKeyGate.isRateLimited(ex)) {
// 429 时把同 key 的下一次许可整体后移,避免所有调用一起再撞
LlmKeyGate.penalizeRateLimited(resolvedKey, 2_000L);
}
lastFailure = ex;
if (UnreachableCooldown.isConnectFailure(ex)) {
connectFailures++;
@@ -344,6 +361,10 @@ public class SimilarAsinLlmClient {
if (context == null) {
return;
}
if (taskGuard != null && taskGuard.isPaused(context.userId())) {
throw new com.nanri.aiimage.common.exception.TaskPausedException(
com.nanri.aiimage.modules.usersecret.service.UserTaskGuardService.PAUSED_MESSAGE);
}
userSecretUsageService.record(context.userId(), UserSecretModule.SIMILAR_ASIN.key(), 1);
}