feat(cost-guard): 代理/LLM 损耗监控与账号级任务暂停
- 每分钟扫描用量增量/日累计,异常置暂停闸并推站内通知 - 品牌补测、跟价派发、外观激活、品牌检测与两个 LLM 客户端统一接闸 - 同一代理/LLM 密钥被 3 个以上账号共用时给出用户提示 - 品牌检测出口拒绝批级熔断;LLM 同 key 共享并发/间隔/429 惩罚 - V170 新增 biz_user_task_guard
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
package com.nanri.aiimage.common.exception;
|
||||
|
||||
/**
|
||||
* 账号任务已被损耗保护暂停:调用方必须立即停止重试(这不是上游抖动,重试不会变好),
|
||||
* 保留已完成数据,等用户在客户端点「继续任务」后再重跑剩余部分。
|
||||
*/
|
||||
public class TaskPausedException extends RuntimeException {
|
||||
|
||||
public TaskPausedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package com.nanri.aiimage.common.retry;
|
||||
|
||||
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;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* 同一把 LLM key 的进程内全局请求门:并发上限 + 最小间隔 + 429 共享冷却。
|
||||
*
|
||||
* <p>多个账号配同一把 key 时,provider 的 RPM/TPM 是共享的;如果每个调用各自
|
||||
* 重试,429 会互相放大。这里把并发和节奏收到 key 级别,让排队替代重试:
|
||||
* 正常时几乎不等待,打满时后来者按顺序等,避免全员 429。
|
||||
*
|
||||
* <p>配置(环境变量,默认保守;按 provider 实际额度调):
|
||||
* <ul>
|
||||
* <li>{@code AIIMAGE_LLM_KEY_MAX_CONCURRENCY}:同 key 最大在途数,默认 8</li>
|
||||
* <li>{@code AIIMAGE_LLM_KEY_MIN_INTERVAL_MS}:同 key 两次请求最小间隔,默认 50ms</li>
|
||||
* <li>{@code AIIMAGE_LLM_KEY_ACQUIRE_TIMEOUT_MS}:排队等待上限,默认 120000ms</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>只存 key 的 SHA-256 指纹,不把原文写进 map/日志。
|
||||
*/
|
||||
@Slf4j
|
||||
public final class LlmKeyGate {
|
||||
|
||||
private static final int MAX_CONCURRENCY = positiveInt("AIIMAGE_LLM_KEY_MAX_CONCURRENCY", 8);
|
||||
private static final long MIN_INTERVAL_NANOS =
|
||||
TimeUnit.MILLISECONDS.toNanos(positiveLong("AIIMAGE_LLM_KEY_MIN_INTERVAL_MS", 50L));
|
||||
private static final long ACQUIRE_TIMEOUT_NANOS =
|
||||
TimeUnit.MILLISECONDS.toNanos(positiveLong("AIIMAGE_LLM_KEY_ACQUIRE_TIMEOUT_MS", 120_000L));
|
||||
|
||||
private static final ConcurrentHashMap<String, Gate> GATES = new ConcurrentHashMap<>();
|
||||
|
||||
private LlmKeyGate() {
|
||||
}
|
||||
|
||||
/** 同 key 的排队许可;close 必须调用(try-with-resources 自动归还)。 */
|
||||
public static final class Permit implements AutoCloseable {
|
||||
private final Gate gate;
|
||||
|
||||
private Permit(Gate gate) {
|
||||
this.gate = gate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
gate.release();
|
||||
}
|
||||
}
|
||||
|
||||
/** 排队超时:这是本地容量结论,重试只会继续排队,调用方应停止阶梯。 */
|
||||
public static final class QueueTimeoutException extends IllegalStateException {
|
||||
public QueueTimeoutException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static Permit acquire(String apiKey) {
|
||||
Gate gate = GATES.computeIfAbsent(fingerprint(apiKey), ignored -> new Gate());
|
||||
return gate.acquire();
|
||||
}
|
||||
|
||||
/** 429 时把同 key 的下一次许可整体后移,避免所有线程同时再撞一次。 */
|
||||
public static void penalizeRateLimited(String apiKey, long millis) {
|
||||
GATES.computeIfAbsent(fingerprint(apiKey), ignored -> new Gate())
|
||||
.penalize(TimeUnit.MILLISECONDS.toNanos(Math.max(0L, millis)));
|
||||
}
|
||||
|
||||
/** 从异常链里识别 HTTP 429;其他异常(超时/5xx)不改变 key 级节奏。 */
|
||||
public static boolean isRateLimited(Throwable error) {
|
||||
Throwable current = error;
|
||||
while (current != null) {
|
||||
if (current instanceof HttpStatusCodeException status
|
||||
&& status.getStatusCode().value() == 429) {
|
||||
return true;
|
||||
}
|
||||
current = current.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void resetForTests() {
|
||||
GATES.clear();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Gate {
|
||||
private final Semaphore permits = new Semaphore(MAX_CONCURRENCY);
|
||||
private final Object lock = new Object();
|
||||
private final AtomicLong nextAtNanos = new AtomicLong();
|
||||
|
||||
Permit acquire() {
|
||||
try {
|
||||
if (!permits.tryAcquire(ACQUIRE_TIMEOUT_NANOS, TimeUnit.NANOSECONDS)) {
|
||||
throw new QueueTimeoutException(
|
||||
"LLM key 排队超时(同 key 并发已满,等待上限 "
|
||||
+ TimeUnit.NANOSECONDS.toMillis(ACQUIRE_TIMEOUT_NANOS) + "ms)");
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("LLM key 排队被中断", ex);
|
||||
}
|
||||
try {
|
||||
waitForInterval();
|
||||
return new Permit(this);
|
||||
} catch (RuntimeException ex) {
|
||||
permits.release();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
void release() {
|
||||
permits.release();
|
||||
}
|
||||
|
||||
void penalize(long nanos) {
|
||||
if (nanos <= 0) {
|
||||
return;
|
||||
}
|
||||
long now = System.nanoTime();
|
||||
nextAtNanos.accumulateAndGet(now + nanos, Math::max);
|
||||
}
|
||||
|
||||
private void waitForInterval() {
|
||||
long interval = MIN_INTERVAL_NANOS;
|
||||
while (interval > 0) {
|
||||
long now = System.nanoTime();
|
||||
long next = nextAtNanos.get();
|
||||
long wait = next - now;
|
||||
if (wait <= 0) {
|
||||
long assigned = now + interval;
|
||||
if (nextAtNanos.compareAndSet(next, assigned)) {
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
TimeUnit.NANOSECONDS.sleep(Math.min(wait, TimeUnit.MILLISECONDS.toNanos(50)));
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("LLM key 间隔等待被中断", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user