/** * Key429Tracker - 基于 key 的 429 错误追踪和黑名单管理 * * 功能: * - 记录每个 key/token 的 429 错误时间和连续错误次数 * - 实现黑名单机制:连续 3 次 429 且间隔 > 1 分钟的 key 会被拉黑 * - 提供 key 健康状态查询和管理 * - 自动清理过期的错误记录 */ export interface Key429ErrorRecord { key: string; timestamp: number; consecutiveCount: number; lastErrorTime: number; isBlacklisted: boolean; blacklistedAt?: number; pipelineIds: string[]; } export interface Key429TrackerConfig { maxConsecutiveErrors: number; minIntervalMs: number; blacklistDurationMs: number; cleanupIntervalMs: number; maxRecordAgeMs: number; } export declare class Key429Tracker { private errorRecords; private config; private cleanupTimer?; constructor(config?: Partial); /** * 记录 429 错误 * @param key 产生错误的 key * @param pipelineIds 受影响的流水线 ID 列表 * @returns 错误记录和是否触发黑名单 */ record429Error(key: string, pipelineIds: string[]): { record: Key429ErrorRecord; blacklisted: boolean; shouldRetry: boolean; }; /** * 检查 key 是否需要被拉黑 */ private shouldBlacklist; /** * 检查 key 是否可用 */ isKeyAvailable(key: string): boolean; /** * 获取 key 的冷却时间(毫秒) */ getKeyCooldownTime(key: string): number; /** * 获取所有受影响的流水线 ID */ getAffectedPipelineIds(key: string): string[]; /** * 获取所有被拉黑的 key */ getBlacklistedKeys(): string[]; /** * 获取 key 的错误统计 */ getKeyStats(key: string): { totalErrors: number; consecutiveErrors: number; isBlacklisted: boolean; lastErrorTime: number; blacklistedAt?: number; } | null; /** * 重置 key 的错误记录 */ resetKey(key: string): void; /** * 重置所有错误记录 */ resetAll(): void; /** * 启动清理定时器 */ private startCleanupTimer; /** * 清理过期的错误记录 */ private cleanup; /** * 停止清理定时器 */ stop(): void; /** * 获取调试信息 */ getDebugInfo(): { totalRecords: number; blacklistedKeys: string[]; config: Key429TrackerConfig; records: Array<{ key: string; consecutiveCount: number; isBlacklisted: boolean; lastErrorTime: number; }>; }; }