fix: 优化 API Key 并发控制机制

- 调整并发计数器过期时间为3分钟,支持长时间流式请求
- 为流式响应添加客户端断开检测,确保计数正确减少
- 添加响应关闭和错误事件监听器,防止并发计数泄漏
- 提高系统稳定性和资源管理准确性

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
shaw
2025-07-16 11:10:44 +08:00
parent de0a2f685a
commit f9bc2ddb23
2 changed files with 19 additions and 2 deletions

View File

@@ -721,8 +721,10 @@ class RedisClient {
const key = `concurrency:${apiKeyId}`;
const count = await this.client.incr(key);
// 设置过期时间为5分钟,防止计数器永远不清零
await this.client.expire(key, 300);
// 设置过期时间为180秒3分钟,防止计数器永远不清零
// 正常情况下请求会在完成时主动减少计数,这只是一个安全保障
// 180秒足够支持较长的流式请求
await this.client.expire(key, 180);
logger.database(`🔢 Incremented concurrency for key ${apiKeyId}: ${count}`);
return count;