fix: detect secondary rate limit via response body marker#2267
Open
zhaomoran wants to merge 1 commit into
Open
fix: detect secondary rate limit via response body marker#2267zhaomoran wants to merge 1 commit into
zhaomoran wants to merge 1 commit into
Conversation
GitHub does not always return Retry-After or gh-limited-by headers when a secondary rate limit is hit. In those cases the response is a plain 403 whose body contains "You have exceeded a secondary rate limit", which currently slips through GitHubAbuseLimitHandler#isError and surfaces as a generic HttpException. Fall back to matching the "secondary rate" marker in the response body when neither header is present. The body for non-200 responses is already buffered into memory by GitHubConnectorResponse, so this adds no extra I/O for downstream handlers. Fixes hub4j#2009
8614ced to
86f32e8
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2267 +/- ##
============================================
+ Coverage 85.30% 85.31% +0.01%
- Complexity 2553 2555 +2
============================================
Files 241 241
Lines 7540 7547 +7
Branches 396 397 +1
============================================
+ Hits 6432 6439 +7
Misses 872 872
Partials 236 236 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Adds a body-based fallback to
GitHubAbuseLimitHandler#isErrorso that 403 responses carrying the "secondary rate" marker in the body are recognized as secondary rate limit errors, even when neitherRetry-Afternorgh-limited-byis present.Fixes #2009
Why
GitHub does not always return
Retry-Afterorgh-limited-byheaders when a secondary rate limit is hit. As reported in #2009, endpoints like/search/issuescan respond with a plain403, no rate-limit headers, and only the body explains:That response currently slips through
GitHubAbuseLimitHandler#isError, soRetryRequestExceptionis never thrown and the request just fails with a genericHttpException.GitHub's official guidance on rate-limit errors explicitly states that
Retry-Aftermay be missing, andgh-limited-byis not part of the documented response headers at all, so we cannot rely solely on headers.How
When the status is 403 and neither
Retry-Afternorgh-limited-byis set, read the response body and match\bsecondary rate\b(case-insensitive). The matching pattern mirrors octokit/plugin-throttling's/\bsecondary rate\b/i, which has been stable for years.There is no extra I/O cost:
GitHubConnectorResponse#bodyStreamalready buffers non-200 bodies into a byte array on the first read, so the body is read once and reused by both this check and any downstream handler that constructs anHttpException. This is the same pattern already used bySTATUS_HTTP_BAD_REQUEST_OR_GREATERto detect Unicorn pages.Tests
Added
testHandler_Wait_Secondary_Limits_Body_Marker_Only, a WireMock scenario reproducing the #2009 case — a 403 with noRetry-After, nogh-limited-by, and the body marker — and asserts:isErrorreturns true (otherwise the handler is never called)parseWaitTimefalls back toDEFAULT_WAIT_MILLIS(61s, matching GitHub's "wait at least one minute" guidance)All 10 tests in
AbuseLimitHandlerTestpass locally.Before submitting a PR:
@linkJavaDoc entries to the relevant documentation on https://docs.github.com/en/rest .mvn -D enable-ci clean install site "-Dsurefire.argLine=--add-opens java.base/java.net=ALL-UNNAMED"locally. If this command doesn't succeed, your change will not pass CI.main. You will create your PR from that branch.When creating a PR: