Skip to content

Feat/rate limit optimization#359

Merged
ding113 merged 5 commits intodevfrom
feat/rate-limit-optimization
Dec 18, 2025
Merged

Feat/rate limit optimization#359
ding113 merged 5 commits intodevfrom
feat/rate-limit-optimization

Conversation

@NightYuYyy
Copy link
Collaborator

feat: 优化限额管理系统检查顺序和错误响应

核心改进

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

NightYu and others added 3 commits December 18, 2025 15:32
## 核心改进

### 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
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • 限额检查顺序优化: 实现了基于硬上限优先、短窗口优先、细粒度优先原则的11步科学检查顺序,并支持Key和User混合检查。
  • 新增用户层限额支持: 扩展了限额服务以支持用户层面的5小时、周和月消费限额,并增加了相应的数据库查询功能。
  • 错误响应机制改进: 优化了错误响应的状态码(RPM/并发使用429,消费限额使用402),统一添加了"code"字段,并修正了用户类型的错误文案。
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added the enhancement New feature or request label Dec 18, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions bot added the size/M Medium PR (< 500 lines) label Dec 18, 2025
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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
  2. User Cost Limits (service.ts): The type extension from "key" | "provider" to "key" | "provider" | "user" is properly implemented with correct branching logic.

  3. Database Query (statistics.ts): The new sumUserCostInTimeRange() function follows the established pattern of existing functions (sumKeyCostInTimeRange, sumProviderCostInTimeRange).

  4. HTTP Status Codes (error-handler.ts): The distinction between 429 (rate/concurrent limits) and 402 (cost limits) is semantically appropriate and well-documented.

  5. Response Codes (responses.ts): The addition of 402 error type handling and the getErrorCode() method provides consistent error code generation.

Design Observations:

  • The repetitive check pattern in rate-limit-guard.ts prioritizes 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
@github-actions
Copy link
Contributor

🧪 测试结果

测试类型 状态
代码质量
单元测试
集成测试
API 测试

总体结果: ✅ 所有测试通过

- 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>
@NightYuYyy
Copy link
Collaborator Author

已完成所有优化建议

感谢 Gemini Code Assist 的建议!我已经完成了以下优化:

1. ✅ 提取重复的状态码计算逻辑

  • 新增 helper 函数
  • 统一处理 RPM/并发(429)vs 消费限额(402)的逻辑
  • 代码更简洁、可维护性更强

2. ✅ 重构 regex 解析逻辑

  • 新增通用的 函数
  • 替换了 4 个重复的解析方法
  • 代码行数减少约 50 行

3. ✅ 用 switch 替换嵌套三元运算符

  • 在 中使用 switch
  • 在 中使用 switch
  • 提高代码可读性和可扩展性

提交记录: af130ac

所有优化已推送并等待 CI 验证。

@github-actions
Copy link
Contributor

🧪 测试结果

测试类型 状态
代码质量
单元测试
集成测试
API 测试

总体结果: ✅ 所有测试通过

@ding113 ding113 merged commit 380ebf3 into dev Dec 18, 2025
8 checks passed
@github-project-automation github-project-automation bot moved this from Backlog to Done in Claude Code Hub Roadmap Dec 18, 2025
@NightYuYyy NightYuYyy deleted the feat/rate-limit-optimization branch December 18, 2025 09:10
@github-actions github-actions bot mentioned this pull request Dec 19, 2025
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size/M Medium PR (< 500 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants

Comments