Conversation
## 核心改进 ### 1. 限额检查顺序优化(基于 Codex 专业分析) - 实现 11 步科学检查顺序: 1-2. 永久硬限制:Key 总限额 → User 总限额 3-4. 资源/频率保护:Key 并发 → User RPM 5-7. 短期周期限额:Key 5h → User 5h → User 每日 8-11. 中长期周期限额:Key 周 → User 周 → Key 月 → User 月 - 实现 Key/User 混合检查(同一窗口 Key → User 交替) - 设计原则:硬上限优先、短窗口优先、细粒度优先 ### 2. 新增 User 层 5h/周/月限额支持 - 扩展 RateLimitService.checkCostLimits 支持 user 类型 - 新增 sumUserCostInTimeRange() 数据库查询函数 - 支持 User 层多维度限额控制 ### 3. 错误响应优化 - 状态码优化:RPM/并发用 429,消费限额用 402 - 所有错误统一添加 code 字段 - 修正 User 类型的错误文案 ## 测试验证 - ✅ 11 步检查顺序全部验证通过 - ✅ 混合检查逻辑正确 - ✅ 错误响应格式统一 - ✅ Codex 代码审核通过(8/10) ## 相关文件 - src/app/v1/_lib/proxy/rate-limit-guard.ts - src/lib/rate-limit/service.ts - src/repository/statistics.ts - src/app/v1/_lib/proxy/error-handler.ts - src/app/v1/_lib/proxy/responses.ts Co-authored-by: Codex <gpt-5.2@openai.com>
Remove explicit 'number' type annotation that conflicts with Recharts' Formatter type definition, which expects 'number | undefined'. CI Run: https://github.com/ding113/claude-code-hub/actions/runs/20329503404
Summary of ChangesHello @NightYuYyy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在全面优化限额管理系统,通过引入更精细和逻辑化的11步检查流程,确保限额判断的准确性和效率。同时,它扩展了用户层面的消费限额控制,并改进了错误响应的清晰度和标准化,从而提升了系统的健壮性和用户体验。 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request significantly refactors the rate limiting mechanism, introducing a more granular and prioritized check order for various limits (Key Total, User Total, Key Concurrent Sessions, User RPM, Key 5h, User 5h, User Daily, Key Weekly, User Weekly, Key Monthly, User Monthly). It also enhances error handling by differentiating HTTP status codes for rate limit errors, returning 429 for RPM/concurrency limits and 402 for consumption limits, and adds a new getErrorCode helper to generate specific error codes. Furthermore, the changes extend the checkCostLimits service to support user-level cost limits, backed by a new sumUserCostInTimeRange database function. Review comments highlight areas for improvement, suggesting the extraction of duplicate status code calculation logic into a helper function in error-handler.ts, refactoring common regex parsing logic in rate-limit-guard.ts into a shared utility, and replacing a nested ternary operator with a switch statement for better readability and extensibility in rate-limit/service.ts when determining the cost summation function based on type.
There was a problem hiding this comment.
Code Review Summary
This PR implements a comprehensive rate limit optimization with a well-designed 11-step check ordering and extends user-level quota support. The implementation follows existing patterns consistently.
PR Size: M (Medium)
- Lines changed: 547 (401 additions, 146 deletions)
- Files changed: 6
Issues Found
| Category | Critical | High | Medium | Low |
|---|---|---|---|---|
| Logic/Bugs | 0 | 0 | 0 | 0 |
| Security | 0 | 0 | 0 | 0 |
| Error Handling | 0 | 0 | 0 | 0 |
| Types | 0 | 0 | 0 | 0 |
| Comments/Docs | 0 | 0 | 0 | 0 |
| Tests | 0 | 0 | 0 | 0 |
| Simplification | 0 | 0 | 0 | 0 |
Analysis Notes
Key Changes Reviewed:
-
Rate Limit Check Ordering (
rate-limit-guard.ts): The new 11-step check sequence is logically sound:- Hard limits (Key/User total) first
- Resource/frequency protection (concurrent sessions, RPM) second
- Short-term windows (5h, daily) third
- Long-term windows (weekly, monthly) last
-
User Cost Limits (
service.ts): The type extension from"key" | "provider"to"key" | "provider" | "user"is properly implemented with correct branching logic. -
Database Query (
statistics.ts): The newsumUserCostInTimeRange()function follows the established pattern of existing functions (sumKeyCostInTimeRange,sumProviderCostInTimeRange). -
HTTP Status Codes (
error-handler.ts): The distinction between 429 (rate/concurrent limits) and 402 (cost limits) is semantically appropriate and well-documented. -
Response Codes (
responses.ts): The addition of 402 error type handling and thegetErrorCode()method provides consistent error code generation.
Design Observations:
- The repetitive check pattern in
rate-limit-guard.tsprioritizes clarity over DRY - each limit check is explicit and self-contained, making the ordering obvious - Error messages properly distinguish between Key and User contexts
Review Coverage
- Logic and correctness - Clean
- Security (OWASP Top 10) - Clean
- Error handling - Clean
- Type safety - Clean
- Documentation accuracy - Clean
- Test coverage - Not assessed (no test files changed)
- Code clarity - Good
Automated review by Claude AI
- Update biome schema version to 2.3.10 for CI compatibility - Add test file overrides to ignore unused variables and imports - Fix import ordering in non-test files
🧪 测试结果
总体结果: ✅ 所有测试通过 |
- Extract rate limit status code calculation to helper function - Refactor regex parsing logic to shared utility function - Replace nested ternary operators with switch statements Co-authored-by: Gemini Code Assist <gemini@google.com>
|
✅ 已完成所有优化建议 感谢 Gemini Code Assist 的建议!我已经完成了以下优化: 1. ✅ 提取重复的状态码计算逻辑
2. ✅ 重构 regex 解析逻辑
3. ✅ 用 switch 替换嵌套三元运算符
提交记录: af130ac 所有优化已推送并等待 CI 验证。 |
🧪 测试结果
总体结果: ✅ 所有测试通过 |
feat: 优化限额管理系统检查顺序和错误响应
核心改进
1. 限额检查顺序优化(基于 Codex 专业分析)
1-2. 永久硬限制:Key 总限额 → User 总限额
3-4. 资源/频率保护:Key 并发 → User RPM
5-7. 短期周期限额:Key 5h → User 5h → User 每日
8-11. 中长期周期限额:Key 周 → User 周 → Key 月 → User 月
2. 新增 User 层 5h/周/月限额支持
3. 错误响应优化
测试验证
相关文件
Co-authored-by: Codex gpt-5.2@openai.com