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;

View File

@@ -45,6 +45,21 @@ router.post('/v1/messages', authenticateApiKey, async (req, res) => {
res.setHeader('Connection', 'keep-alive');
res.setHeader('Access-Control-Allow-Origin', '*');
// 为流式响应添加客户端断开检测,确保并发计数正确减少
if (req.concurrencyInfo) {
// 添加响应关闭事件监听器
res.on('close', () => {
logger.api(`🔌 Stream response closed for key: ${req.apiKey.id} (${req.apiKey.name}), triggering concurrency decrement`);
req.concurrencyInfo.decrementConcurrency();
});
// 添加错误事件监听器
res.on('error', (error) => {
logger.api(`⚠️ Stream response error for key: ${req.apiKey.id} (${req.apiKey.name}): ${error.message}`);
req.concurrencyInfo.decrementConcurrency();
});
}
let usageDataCaptured = false;
// 使用自定义流处理器来捕获usage数据