Add Discord rate limiting with exponential backoff retry mechanism
@mahek2016 已经在做这个了。
开始于 2026年2月22日。
评估
调研方向
首先阅读 backend/bot.py 以及其中引用的 Discord 速率限制文档,然后检查 issue 中提出的 backend/rate_limiter.py、backend/discord_client.py 和测试文件。根据列出的重试、Redis、日志记录、排队、性能和覆盖率要求验证设计。完成的标准是现有 bot 使用增强后的 client,并且所有列出的测试和验收标准都通过。
由索引模型根据 Issue 内容生成。
描述
Is your feature request related to a problem?
- Yes, it is related to a problem
Describe the feature you'd like
Problem
The Discord bot lacks proper rate limit handling:
- 429 errors occur when hitting Discord API rate limits
- No recovery mechanism - errors just propagate to user
- Poor user experience - message send failures without auto-retry
- Silent failures - no logging of rate limit incidents
Current Behavior
User command → Bot tries to send → 429 Error → User sees "something went wrong"
Desired Behavior
User command → Bot tries to send → 429 Error → Auto-retry with exponential backoff → Success
Solution Overview
Implement a robust rate limiting system with:
- Rate limit tracking per Discord endpoint/bucket
- Exponential backoff retry logic (2^n + jitter)
- Command queueing during rate limits
- Comprehensive logging and metrics
- Graceful degradation with user-friendly messages
Implementation Details
Component 1: Rate Limiter Core
- Create
backend/rate_limiter.py - Implement
DiscordRateLimiterclass with:wait_if_rate_limited()- Check if endpoint is rate limitedexecute_with_retry()- Execute with automatic retry- Exponential backoff: 2^attempt × retry_after + jitter
- Maximum 3 retry attempts
Component 2: Discord Client Wrapper
- Create
backend/discord_client.py - Implement
EnhancedDiscordClientclass with:send_message_with_retry()- Send with rate limit handlingedit_message_with_retry()- Edit with rate limit handling- Error handling for 429 responses
- User-friendly error messages
Component 3: Redis Integration
- Use Redis to track rate limit state across bot instances
- Store:
discord_ratelimit:bucket:remaining,discord_ratelimit_reset:bucket - Per-endpoint tracking (channels, reactions, etc.)
Files to Create
backend/
├── rate_limiter.py (180 lines) - Core rate limiting logic
├── discord_client.py (250 lines) - Enhanced Discord client
└── discord_utils.py (80 lines) - Helper utilities
tests/
├── test_rate_limiter.py (200 lines) - Rate limiter tests
├── test_discord_client.py (150 lines) - Client tests
└── fixtures/discord_mocks.py (100 lines) - Test fixtures
docs/
└── DISCORD_RATE_LIMITING.md (150 lines) - Usage documentation
Integration Points
- Update
backend/bot.pyto use enhanced client (+45 lines) - Add
redis>=4.0.0torequirements.txt - Set environment:
REDIS_URL=redis://localhost:6379
Testing
- 12+ unit tests for rate limiter logic
- Integration tests with mock Discord API
- Exponential backoff calculation verification
- Retry exhaustion handling
- Performance tests (minimal overhead when not rate limited)
Acceptance Criteria
- Rate limits properly detected and handled
- Automatic retry with exponential backoff
- Maximum 3 retry attempts configurable
- Command queueing during rate limits
- Rate limit incidents logged with details
- Zero performance impact when not rate limited
- 90%+ test coverage
- No merge conflicts
- All existing tests still pass
- Documentation included
Performance Requirements
- Overhead when NOT rate limited: <1ms per request
- Successful retry adds latency only when rate limited
- Redis operations are async and non-blocking
- No impact on normal message flow
Migration Guide
For deployments using this:
- Install Redis:
pip install redis>=4.0.0 - Set
REDIS_URLenvironment variable - Start Redis service
- Update code to use
EnhancedDiscordClient - Test in staging first
- Deploy with monitoring
References
- Discord Rate Limit Docs: https://discord.com/developers/docs/topics/rate-limits
- Exponential Backoff: https://en.wikipedia.org/wiki/Exponential_backoff
- Similar implementation in AWS SDK
Related Issues
Fixes: Bot resilience failures when hitting Discord limits
Related to: #48 (general bot reliability)
Nice to Have (Future)
- Prometheus metrics for rate limit tracking
- Rate limit prediction using historical data
- Per-guild rate limiting
- Analytics dashboard
Record
- I agree to follow this project's Code of Conduct
- I want to work on implementing this feature
- 主要语言
- Python
- 星标
- 103
- 派生
- 143
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
AOSSIE-Org/Devr.AI 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 95/100
AOSSIE-Org/Devr.AI#315 · 1 条评论 ·
-
Bump Weaviate image to 1.39.13 (Go 1.26.10 / CVE-2026-111943)可能已有人在做 @dudanogueira 于 38 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
AOSSIE-Org/Devr.AI#309 · 1 条评论 ·
-
DOCS UPDATE: Incorrect Flask command path in backend installation guide可能已有人在做 @Ashwin-Pillai-22 于 321 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 68/100
AOSSIE-Org/Devr.AI#229 · 3 条评论 ·
-
BUG:Devr.AI Agent loses user_profile for returning users and fails to persist conversation summaries & cumulative interaction counts可能已有人在做 @shubhumre777 于 34 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
AOSSIE-Org/Devr.AI#311 · 2 条评论 ·
-
BUG: Local pytest suite fails to collect due to deprecated imports and mock schemas可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 2/5 1-3 小时 新手友好度 25/100
AOSSIE-Org/Devr.AI#307 · 1 条评论 ·
查看 AOSSIE-Org/Devr.AI 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
pimutils/vdirsyncer#1242 ·
-
priority:P2
难度 2/5 1-3 小时 新手友好度 75/100
QuantConnect/lean-cli#792 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
open-webui/open-webui#35360 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
-
Suggest link: Destiny Verify (MIT zero-dependency pre-commit security, citation & link hygiene CLI)未关闭
难度 1/5 1 小时以内 新手友好度 68/100