Add velocity-based rate limiting to prevent GitHub secondary rate limits #1198
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR reworks the rate limiting implementation to prevent GitHub's secondary rate limits, which are based on request velocity (requests/second) rather than total quota. The implementation is split into commits
for easier review:
Commit 1: Refactor existing rate limiter (no behavior change)
"Add proactive GitHub API rate limiting with shared implementation" (
fb6a29d)pkg/httpratelimitpackagegithubreconciler(new usage)Commit 2: Add velocity-based rate limiting (new functionality)
"Add velocity-based rate limiting to prevent GitHub secondary rate limits" (
32a00ad)DefaultMaxRequestsPerSecond = 15.0constantBurstMultiplier = 2constantNewTransport()to acceptmaxRequestsPerSecparameterrate.Inf) to actual RPS-based rate limitingThe Problem
Our previous approach (PR #1195) attempted to proactively throttle based on
X-RateLimit-Remainingheaders. However, testing with a rate limit reproducer revealed that secondary rate limits don't provide thesewarning headers:
Key insight: Secondary limits are triggered by request velocity (req/sec), not total quota. GitHub doesn't provide predictive headers - requests simply fail with 403 + Retry-After when hit.
How It Works
Before:
After:
Example: With 15 RPS limit and 2x burst, we can handle occasional bursts of 30 requests but maintain an average of 15 req/sec, staying well below GitHub's undocumented secondary limit threshold.
Backward Compatibility
Testing
Validated with rate limit reproducer tool that can trigger secondary limits on demand.
Future Work