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
@@ -0,0 +1,52 @@
package com.nanri.aiimage.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 代理 / LLM 损耗保护:按分钟增量与当日累计两条线判定异常,命中即暂停该账号的新任务。
*
* <p>阈值默认值按「正常批次不该碰、失控循环一定碰」取:品牌补测单批最多百来个品牌,
* 一分钟内正常提取几十次;失控时一个账号一分钟能烧到几百上千次。上线后按后台用量
* 曲线调整,不把阈值写死在代码里。
*/
@Data
@ConfigurationProperties(prefix = "aiimage.cost-guard")
public class CostGuardProperties {
/** 总开关:关掉只停自动暂停与提示,记账与状态接口仍可用。 */
private boolean enabled = true;
/** 扫描间隔(毫秒);用量是按分钟增量判定的,默认 60 秒。 */
private long scanIntervalMs = 60_000L;
/** 首次扫描延迟(毫秒):避开应用启动时的迁移与预热。 */
private long scanInitialDelayMs = 30_000L;
/** 展示给用户的窗口长度(秒);扫描间隔默认 60 秒,含义是「最近约一分钟增量」。 */
private int windowSeconds = 60;
/** 窗口内代理提取次数阈值;超过即暂停。 */
private int proxyWindowThreshold = 600;
/** 窗口内 LLM 调用次数阈值。 */
private int llmWindowCallsThreshold = 180;
/** 窗口内 LLM token 阈值(prompt + completion)。 */
private long llmWindowTokensThreshold = 600_000L;
/** 当日代理提取次数上限;0=关闭。 */
private int dailyProxyThreshold = 8_000;
/** 当日 LLM 调用次数上限;0=关闭。 */
private int dailyLlmCallsThreshold = 6_000;
/** 当日 LLM token 上限;0=关闭。 */
private long dailyLlmTokensThreshold = 20_000_000L;
/** 同一把代理/LLM 密钥被多少个账号共用时提示;0=关闭。 */
private int sharedAccountsThreshold = 3;
/** 共享密钥扫描间隔(毫秒);首轮随第一次损耗扫描一起跑。 */
private long sharedScanIntervalMs = 600_000L;
}