Skip to content

Commit

Permalink
Accept 429 as an indication of the rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
holly-cummins committed Jul 4, 2024
1 parent cab9bbb commit 7a6cc42
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/AbuseLimitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
private long parseWaitTime(HttpURLConnection uc) {
String v = uc.getHeaderField("Retry-After");
if (v == null)
return 60 * 1000; // can't tell, return 1 min
return 61 * 1000; // can't tell, return 1 min plus 1 second for luck

Check warning on line 92 in src/main/java/org/kohsuke/github/AbuseLimitHandler.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kohsuke/github/AbuseLimitHandler.java#L92

Added line #L92 was not covered by tests

return Math.max(1000, Long.parseLong(v) * 1000);
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/kohsuke/github/GitHubAbuseLimitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ public abstract class GitHubAbuseLimitHandler extends GitHubConnectorResponseErr
*/
@Override
boolean isError(@Nonnull GitHubConnectorResponse connectorResponse) {
return isForbidden(connectorResponse) && hasRetryOrLimitHeader(connectorResponse);
return isTooManyRequests(connectorResponse)
|| (isForbidden(connectorResponse) && hasRetryOrLimitHeader(connectorResponse));
}

/**
* Checks if the response status code is TOO_MANY_REQUESTS (403).
*
* @param connectorResponse
* the response from the GitHub connector
* @return true if the status code is TOO_MANY_REQUESTS
*/
private boolean isTooManyRequests(GitHubConnectorResponse connectorResponse) {
return connectorResponse.statusCode() == TOO_MANY_REQUESTS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
*/
abstract class GitHubConnectorResponseErrorHandler {

/**
* The HTTP 429 Too Many Requests response status code indicates the user has sent too many requests in a given
* amount of time ("rate limiting").
*
* A Retry-After header might be included to this response indicating how long to wait before making a new request.
*
* Why is this hardcoded here? The HttpURLConnection class is missing the status codes above 415, so the constant
* needs to be sourced from elsewhere.
*/
public static final int TOO_MANY_REQUESTS = 429;

/**
* Called to detect an error handled by this handler.
*
Expand Down

0 comments on commit 7a6cc42

Please sign in to comment.