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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user