fix(rate-limit): return structured JSON for 429 responses - #6560
Conversation
WalkthroughThe rate-limit middleware now returns localized JSON 429 responses. New i18n keys and translations support three locales. Tests cover response content, headers, critical limiter settings, and Redis namespace behavior. ChangesRate-limit localization
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant CriticalRateLimit
participant writeRateLimited
participant i18n
Client->>CriticalRateLimit: Send request
CriticalRateLimit->>writeRateLimited: Reject exceeded request
writeRateLimited->>i18n: Resolve MsgRateLimitExceeded
i18n-->>writeRateLimited: Return localized message
writeRateLimited-->>Client: Return JSON 429 and Retry-After
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@middleware/rate_limit_test.go`:
- Around line 49-84: Add a separate deterministic test for CriticalRateLimit
with common.RedisEnabled disabled, reusing the router/request setup and
restoring the global setting afterward. Drive the in-memory limiter to its final
allowed request and assert the 429 response includes the configured Retry-After
header, application/json; charset=utf-8 content type, and the localized JSON
body. Keep the existing Redis test unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: ed1e33e4-3271-488b-a614-121103b63f0e
📒 Files selected for processing (6)
i18n/keys.goi18n/locales/en.yamli18n/locales/zh-CN.yamli18n/locales/zh-TW.yamlmiddleware/rate-limit.gomiddleware/rate_limit_test.go
📝 变更描述 / Description
通用限流中间件的
writeRateLimited当前只返回 HTTP 429 和Retry-After,响应体为空。浏览器/API 客户端若按 JSON 解析错误响应,会得到二次解析错误,无法向用户展示真正的限流原因。本 PR 保留现有状态码和
Retry-After行为,同时返回 dashboard API 已使用的稳定 JSON 结构:{ "success": false, "message": "Too many requests. Please try again later." }错误消息通过现有后端 i18n 机制支持英文、简体中文和繁体中文。该修复同时覆盖 Redis 与内存后端,以及共用
writeRateLimited的 Global Web/API、Critical、Download、Upload 和 Search 限流。This is intentionally separate from #6528: that PR fixes the model-specific in-memory limiter, while this PR fixes the shared IP/user limiter response used by
CriticalRateLimitand the other generic rate-limit middleware.🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
回归测试验证:
Retry-After;Content-Type为 JSON;success: false和可读、可本地化的message。Summary by CodeRabbit
New Features
Retry-Afterheader continues to provide retry guidance.Bug Fixes