Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
OpenAI 故障转移机制
概述
OpenAI接口现已实现完整的故障转移机制,参照Claude接口的设计模式,支持自动错误计数、临时禁用和自动恢复功能。
核心功能
1. 请求成功判定
2. 错误计数机制
3. 临时禁用
4. 自动恢复
配置参数
环境变量配置(
.env文件):架构设计
请求流程:
单体测试
这个脚本会:
测试内容包括:
支持的账户类型
本故障转移机制覆盖以下类型:
两者共享同一组配置(OPENAI_ERROR_THRESHOLD、OPENAI_ERROR_WINDOW_MINUTES、OPENAI_TEMP_DISABLE_MINUTES),但使用独立的 Redis 命名空间,互不影响。
Redis Key 命名
openai_account:request_errors:{id}、openai_account:temp_error:{id}、openai_account:temp_error:lock:{id}openai_responses_account:request_errors:{id}、openai_responses_account:temp_error:{id}、openai_responses_account:temp_error:lock:{id}相关文件
/src/routes/openaiRoutes.js- 路由层错误处理/src/services/openaiAccountService.js- 故障转移核心逻辑/src/services/openaiCleanupService.js- 定期清理服务(并行处理 openai 与 openai-responses)/src/services/rateLimitCleanupService.js- 限流清理服务/scripts/quick-test-failover.sh- 快速测试脚本(调用手动测试脚本)/scripts/test-openai-failover.js- 手动测试脚本 for openai/scripts/test-openai-responses-failover.js- 手动测试脚本 for openai-responses