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.appearancepatent.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.AppearancePatentProperties;
@@ -156,6 +157,10 @@ public class AppearancePatentLlmClient {
private final ExternalCallMetricsRecorder externalCallMetrics;
private final BrandCheckClient brandCheckClient;
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;
@@ -656,11 +661,21 @@ public class AppearancePatentLlmClient {
Exception lastFailure = null;
AtomicInteger connectFailures = new AtomicInteger();
for (int attempt = 1; attempt <= attempts; attempt++) {
try {
try (LlmKeyGate.Permit ignored = LlmKeyGate.acquire(apiKey)) {
String content = invokeChatOnce(model, system, userText, images, apiKey, 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) {
throw (LlmKeyGate.QueueTimeoutException) ex;
}
if (LlmKeyGate.isRateLimited(ex)) {
LlmKeyGate.penalizeRateLimited(apiKey, 2_000L);
}
lastFailure = ex;
if (UnreachableCooldown.isConnectFailure(ex)) {
boolean entered = unreachableCooldown.recordConnectFailure(host, cooldownMillis);
@@ -1351,6 +1366,10 @@ public class AppearancePatentLlmClient {
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.APPEARANCE_PATENT.key(), 1);
}
@@ -109,6 +109,10 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
private final TaskProgressLightAssembler taskProgressLightAssembler;
private final UserApiSecretService userApiSecretService;
/** 损耗闸:字段注入避免改动手工构造器与 golden 用例;null=未启用(单测)。 */
@org.springframework.beans.factory.annotation.Autowired(required = false)
private com.nanri.aiimage.modules.usersecret.service.UserTaskGuardService taskGuard;
/**
* 参数顺序=迁移前 {@code @RequiredArgsConstructor} 的字段声明顺序,一字未动;
* 其中五枚(fileTaskMapper/fileResultMapper/taskDistributedLockService/
@@ -196,6 +200,10 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
@Transactional
public void activateTask(Long taskId, Long userId) {
if (taskGuard != null && taskGuard.isPaused(userId)) {
throw new com.nanri.aiimage.common.exception.BusinessException(com.nanri.aiimage.modules
.usersecret.service.UserTaskGuardService.PAUSED_MESSAGE);
}
recordSupport().activateTask(taskId, userId);
}