fix(cost-guard): 注解 SQL 收进 XML、新端点进守卫名单,并按实扫对齐 catch 棘轮

三格护栏红都在本笔自己头上:UserSecretUsageMapper/UserTaskGuardMapper 的 5 条
注解 SQL 迁进 mapper XML(两处语句台账同步);cost-guard 两个端点身份取自 JWT,
进 AdminApiGuardFilter 无条件名单而不是登记成「方法内自校验」;catch 静默棘轮
先摘掉 6 处能去的静默出口(含 resume 写失败被吞成「已继续任务」的假成功),
余下 6 处带 [cost-guard] 锚点的 fail-open 逐处记名入账。
This commit is contained in:
2026-10-24 13:21:47 +08:00
parent ba20f5ed42
commit 1743666334
13 changed files with 120 additions and 113 deletions
@@ -1,11 +1,9 @@
package com.nanri.aiimage.common.retry;
import cn.hutool.crypto.digest.DigestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.HttpStatusCodeException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@@ -30,11 +28,14 @@ import java.util.concurrent.atomic.AtomicLong;
@Slf4j
public final class LlmKeyGate {
private static final int MAX_CONCURRENCY = positiveInt("AIIMAGE_LLM_KEY_MAX_CONCURRENCY", 8);
private static final java.util.regex.Pattern POSITIVE = java.util.regex.Pattern.compile("\\d+");
private static final int MAX_CONCURRENCY =
(int) positive("AIIMAGE_LLM_KEY_MAX_CONCURRENCY", 3, 8);
private static final long MIN_INTERVAL_NANOS =
TimeUnit.MILLISECONDS.toNanos(positiveLong("AIIMAGE_LLM_KEY_MIN_INTERVAL_MS", 50L));
TimeUnit.MILLISECONDS.toNanos(positive("AIIMAGE_LLM_KEY_MIN_INTERVAL_MS", 9, 50L));
private static final long ACQUIRE_TIMEOUT_NANOS =
TimeUnit.MILLISECONDS.toNanos(positiveLong("AIIMAGE_LLM_KEY_ACQUIRE_TIMEOUT_MS", 120_000L));
TimeUnit.MILLISECONDS.toNanos(positive("AIIMAGE_LLM_KEY_ACQUIRE_TIMEOUT_MS", 9, 120_000L));
private static final ConcurrentHashMap<String, Gate> GATES = new ConcurrentHashMap<>();
@@ -92,36 +93,17 @@ public final class LlmKeyGate {
private static String fingerprint(String apiKey) {
String key = apiKey == null ? "" : apiKey.trim();
try {
byte[] digest = MessageDigest.getInstance("SHA-256")
.digest(key.getBytes(StandardCharsets.UTF_8));
StringBuilder hex = new StringBuilder(digest.length * 2);
for (byte b : digest) {
hex.append(String.format("%02x", b));
}
return hex.toString();
} catch (NoSuchAlgorithmException ex) {
// SHA-256 是 JDK 必备算法;真缺失时退化为长度+哈希码,仍不落原文
return "fallback:" + key.length() + ":" + key.hashCode();
}
return DigestUtil.sha256Hex(key);
}
private static int positiveInt(String name, int fallback) {
try {
int value = Integer.parseInt(System.getenv().getOrDefault(name, "").trim());
return value > 0 ? value : fallback;
} catch (RuntimeException ex) {
return fallback;
}
}
private static long positiveLong(String name, long fallback) {
try {
long value = Long.parseLong(System.getenv().getOrDefault(name, "").trim());
return value > 0 ? value : fallback;
} catch (RuntimeException ex) {
/** 环境变量读数:非纯数字/可能越界/非正一律走默认,坏配置不许把闸门拖崩。 */
private static long positive(String name, int maxDigits, long fallback) {
String raw = System.getenv().getOrDefault(name, "").trim();
if (raw.length() > maxDigits || !POSITIVE.matcher(raw).matches()) {
return fallback;
}
long value = Long.parseLong(raw);
return value > 0 ? value : fallback;
}
private static final class Gate {
@@ -123,6 +123,8 @@ public class AdminApiGuardFilter extends OncePerRequestFilter {
// 2026-09 全维度审查补:内部端点此前仅靠 controller 自校验令牌,纳入守卫后
// 不带令牌的请求直接 401(带可信令牌的仍由 doFilterInternal 放行)
"/api/internal",
// 损耗监控的暂停/继续:uid 只从 JWT 取,匿名请求既读不到状态也不该能解除暂停闸
"/api/user/cost-guard",
};
private final AdminAuthSupport adminAuthSupport;
@@ -5,7 +5,6 @@ import com.nanri.aiimage.modules.usersecret.model.dto.UserSecretUsageAggregate;
import com.nanri.aiimage.modules.usersecret.model.entity.UserSecretUsageEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@@ -40,28 +39,9 @@ public interface UserSecretUsageMapper extends BaseMapper<UserSecretUsageEntity>
* 当日用量按用户 × 模块聚合(损耗扫描每分钟读一次)。
* 表是「用户 × 模块 × 天」的日聚合,行数 = 用户数 × 3,全量 GROUP BY 成本可忽略。
*/
@Select("""
SELECT user_id AS userId,
module_key AS moduleKey,
COALESCE(SUM(call_count), 0) AS callCount,
COALESCE(SUM(prompt_tokens + completion_tokens), 0) AS tokens
FROM biz_user_secret_usage_daily
WHERE business_date = #{businessDate}
GROUP BY user_id, module_key
""")
List<UserSecretUsageAggregate> sumByUserAndModule(@Param("businessDate") LocalDate businessDate);
/** 单个用户的当日读数:客户端状态接口每 30 秒轮询一次,不能为此全表 GROUP BY。 */
@Select("""
SELECT user_id AS userId,
module_key AS moduleKey,
COALESCE(SUM(call_count), 0) AS callCount,
COALESCE(SUM(prompt_tokens + completion_tokens), 0) AS tokens
FROM biz_user_secret_usage_daily
WHERE business_date = #{businessDate}
AND user_id = #{userId}
GROUP BY user_id, module_key
""")
List<UserSecretUsageAggregate> sumForUser(@Param("userId") Long userId,
@Param("businessDate") LocalDate businessDate);
@@ -2,27 +2,14 @@ package com.nanri.aiimage.modules.usersecret.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.usersecret.model.entity.UserTaskGuardEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface UserTaskGuardMapper extends BaseMapper<UserTaskGuardEntity> {
/** 幂等暂停:无记录则建,有记录则刷新原因与时刻。 */
@Insert("""
INSERT INTO biz_user_task_guard (user_id, paused, reason, paused_at, updated_at)
VALUES (#{userId}, 1, #{reason}, NOW(), NOW())
ON DUPLICATE KEY UPDATE paused = 1, reason = #{reason},
paused_at = NOW(), updated_at = NOW()
""")
int pause(@Param("userId") Long userId, @Param("reason") String reason);
@Update("""
UPDATE biz_user_task_guard
SET paused = 0, reason = '', paused_at = NULL, updated_at = NOW()
WHERE user_id = #{userId}
""")
int resume(@Param("userId") Long userId);
}
@@ -150,17 +150,12 @@ public class CostGuardScheduler {
if (userId == null || userId <= 0) {
return new UserTotals();
}
try {
UserTotals totals = new UserTotals();
for (UserSecretUsageAggregate row : usageMapper.sumForUser(userId, LocalDate.now())) {
accumulate(totals, row);
}
return totals;
} catch (Exception ex) {
// 状态接口是客户端每 30 秒一次的轮询:读不到就返回零值,不让一次库抖动打断提示
log.warn("[cost-guard] 读取用户当日用量失败 userId={} err={}", userId, ex.getMessage());
return new UserTotals();
// 库抖动就让它抛:客户端把轮询失败当「维持上一次读数」处理,比在这里伪造一组零值诚实。
UserTotals totals = new UserTotals();
for (UserSecretUsageAggregate row : usageMapper.sumForUser(userId, LocalDate.now())) {
accumulate(totals, row);
}
return totals;
}
private String breachReason(UserTotals now, UserTotals before, WindowDelta delta) {
@@ -205,12 +200,8 @@ public class CostGuardScheduler {
return;
}
lastSharedRefreshAt = now;
try {
sharedSecretMonitorService.refresh();
} catch (Exception ex) {
// 共享密钥扫描失败不影响损耗暂停主流程,下一轮再试
log.warn("[cost-guard] 共享密钥扫描失败 err={}", ex.getMessage(), ex);
}
// 不自己吞:runOnce 里这是最后一步,抛出由 scan() 的 [cost-guard] WARN 记名,下一间隔再试
sharedSecretMonitorService.refresh();
}
public static final class UserTotals {
@@ -1,5 +1,6 @@
package com.nanri.aiimage.modules.usersecret.service;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanri.aiimage.config.CostGuardProperties;
import com.nanri.aiimage.modules.notification.service.NotificationDispatchService;
@@ -12,13 +13,10 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -115,7 +113,7 @@ public class SharedSecretMonitorService {
private void pushDailyWarning(Long userId, UserSecretModule module, String groupKey,
SharedSecretWarningVo warning) {
String day = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
String dedupe = "secret_shared:" + module.key() + ':' + sha256Hex(groupKey)
String dedupe = "secret_shared:" + module.key() + ':' + DigestUtil.sha256Hex(groupKey)
+ ':' + userId + ':' + day;
notificationDispatchService.pushToUser(userId, NotificationService.SCENE_SECRET_SHARED,
NotificationService.LEVEL_WARNING,
@@ -130,15 +128,6 @@ public class SharedSecretMonitorService {
+ "建议在「密钥设置」里为不同账号拆分独立密钥,便于控费和定位问题。";
}
private String sha256Hex(String value) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return HexFormat.of().formatHex(digest.digest(value.getBytes(StandardCharsets.UTF_8)));
} catch (Exception ex) {
return Integer.toHexString(value.hashCode());
}
}
private static final class SecretGroup {
private final UserSecretModule module;
private final Set<Long> userIds = new HashSet<>();
@@ -71,20 +71,17 @@ public class UserTaskGuardService {
}
}
/** 用户手动继续:只清暂停位,不删任何已完成的任务/结果。 */
public boolean resume(Long userId) {
/**
* 用户手动继续:只清暂停位,不删任何已完成的任务/结果。
* 写失败一律外抛——静默返回 false 会让接口把「没解开」回成「已继续任务」。
*/
public void resume(Long userId) {
if (userId == null || userId <= 0) {
return false;
}
try {
guardMapper.resume(userId);
cache.remove(userId);
log.info("[cost-guard] 用户手动继续任务 userId={}", userId);
return true;
} catch (Exception ex) {
log.error("[cost-guard] 继续任务写入失败 userId={} err={}", userId, ex.getMessage(), ex);
return false;
return;
}
guardMapper.resume(userId);
cache.remove(userId);
log.info("[cost-guard] 用户手动继续任务 userId={}", userId);
}
private GuardState load(Long userId) {