Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate-Limit: Thread fix, reset date #160

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp-urlconnection</artifactId>
<version>2.0.0</version>
<version>2.2.0</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/org/kohsuke/github/GHRateLimit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.kohsuke.github;

import java.util.Date;

/**
* Rate limit.
* @author Kohsuke Kawaguchi
Expand All @@ -10,12 +12,28 @@ public class GHRateLimit {
*/
public int remaining;
/**
* Alotted API call per hour.
* Allotted API call per hour.
*/
public int limit;

/**
* The time at which the current rate limit window resets in UTC epoch seconds.
*/
public Date reset;

/**
* Non-epoch date
*/
public Date getResetDate() {
return new Date(reset.getTime() * 1000);
}

@Override
public String toString() {
return remaining+"/"+limit;
return "GHRateLimit{" +
"remaining=" + remaining +
", limit=" + limit +
", resetDate=" + getResetDate() +
'}';
}
}
21 changes: 12 additions & 9 deletions src/main/java/org/kohsuke/github/Requester.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,18 @@ private InputStream wrapStream(HttpURLConnection uc, InputStream in) throws IOEx
* Otherwise throw an exception reporting an error.
*/
/*package*/ void handleApiError(IOException e, HttpURLConnection uc) throws IOException {
if ("0".equals(uc.getHeaderField("X-RateLimit-Remaining"))) {
// API limit reached. wait 10 secs and return normally
try {
Thread.sleep(10000);
return;
} catch (InterruptedException _) {
throw (InterruptedIOException)new InterruptedIOException().initCause(e);
}
}
// Disable this check, because it causes "infinite" thread usage when:
// 1) set bad password you can't apply changed without interrupting thread
// 2) exhausted limit will cause lock upto 1h until reset time
// if ("0".equals(uc.getHeaderField("X-RateLimit-Remaining"))) {
// // API limit reached. wait 10 secs and return normally
// try {
// Thread.sleep(10000);
// return;
// } catch (InterruptedException _) {
// throw (InterruptedIOException)new InterruptedIOException().initCause(e);
// }
// }

if (e instanceof FileNotFoundException)
throw e; // pass through 404 Not Found to allow the caller to handle it intelligently
Expand Down