Add Discord rate limiting with exponential backoff retry mechanism

未关闭
#284 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@mahek2016 已经在做这个了。

开始于 2026年2月22日。

  • #288 来自 @mahek2016 —— 未关闭
  • #289 来自 @anshulchikhale30-p —— 未关闭
  • #302 来自 @Shevilll —— 未关闭

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
python, redis
领域
api, backend

调研方向

首先阅读 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:

  1. Rate limit tracking per Discord endpoint/bucket
  2. Exponential backoff retry logic (2^n + jitter)
  3. Command queueing during rate limits
  4. Comprehensive logging and metrics
  5. Graceful degradation with user-friendly messages

Implementation Details

Component 1: Rate Limiter Core
  • Create backend/rate_limiter.py
  • Implement DiscordRateLimiter class with:
    • wait_if_rate_limited() - Check if endpoint is rate limited
    • execute_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 EnhancedDiscordClient class with:
    • send_message_with_retry() - Send with rate limit handling
    • edit_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.py to use enhanced client (+45 lines)
  • Add redis>=4.0.0 to requirements.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:

  1. Install Redis: pip install redis>=4.0.0
  2. Set REDIS_URL environment variable
  3. Start Redis service
  4. Update code to use EnhancedDiscordClient
  5. Test in staging first
  6. Deploy with monitoring

References

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 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

AOSSIE-Org/Devr.AI 的其他 Issue

查看 AOSSIE-Org/Devr.AI 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。