mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-10 08:21:21 +00:00
Better type safety by splitting RateLimitHandler and AbuseLimitHandler
While the signature is the same, headers that they expect are different, so any non-trivial logic cannot be reused.
This commit is contained in:
63
src/main/java/org/kohsuke/github/AbuseLimitHandler.java
Normal file
63
src/main/java/org/kohsuke/github/AbuseLimitHandler.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package org.kohsuke.github;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
/**
|
||||
* Pluggable strategy to determine what to do when the API abuse limit is hit.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
* @see GitHubBuilder#withAbuseLimitHandler(AbuseLimitHandler)
|
||||
* @see <a href="https://developer.github.com/v3/#abuse-rate-limits">documentation</a>
|
||||
* @see RateLimitHandler
|
||||
*/
|
||||
public abstract class AbuseLimitHandler {
|
||||
/**
|
||||
* Called when the library encounters HTTP error indicating that the API rate limit is reached.
|
||||
*
|
||||
* <p>
|
||||
* Any exception thrown from this method will cause the request to fail, and the caller of github-api
|
||||
* will receive an exception. If this method returns normally, another request will be attempted.
|
||||
* For that to make sense, the implementation needs to wait for some time.
|
||||
*
|
||||
* @see <a href="https://developer.github.com/v3/#rate-limiting">API documentation from GitHub</a>
|
||||
* @param e
|
||||
* Exception from Java I/O layer. If you decide to fail the processing, you can throw
|
||||
* this exception (or wrap this exception into another exception and throw it.)
|
||||
* @param uc
|
||||
* Connection that resulted in an error. Useful for accessing other response headers.
|
||||
*/
|
||||
public abstract void onError(IOException e, HttpURLConnection uc) throws IOException;
|
||||
|
||||
/**
|
||||
* Wait until the API abuse "wait time" is passed.
|
||||
*/
|
||||
public static final AbuseLimitHandler WAIT = new AbuseLimitHandler() {
|
||||
@Override
|
||||
public void onError(IOException e, HttpURLConnection uc) throws IOException {
|
||||
try {
|
||||
Thread.sleep(parseWaitTime(uc));
|
||||
} catch (InterruptedException _) {
|
||||
throw (InterruptedIOException)new InterruptedIOException().initCause(e);
|
||||
}
|
||||
}
|
||||
|
||||
private long parseWaitTime(HttpURLConnection uc) {
|
||||
String v = uc.getHeaderField("Retry-After");
|
||||
if (v==null) return 60 * 1000; // can't tell, return 1 min
|
||||
|
||||
return Math.max(1000, Long.parseLong(v)*1000);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Fail immediately.
|
||||
*/
|
||||
public static final AbuseLimitHandler FAIL = new AbuseLimitHandler() {
|
||||
@Override
|
||||
public void onError(IOException e, HttpURLConnection uc) throws IOException {
|
||||
throw (IOException)new IOException("Abust limit reached").initCause(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -83,7 +83,7 @@ public class GitHub {
|
||||
private final String apiUrl;
|
||||
|
||||
/*package*/ final RateLimitHandler rateLimitHandler;
|
||||
/*package*/ final RateLimitHandler abuseLimitHandler;
|
||||
/*package*/ final AbuseLimitHandler abuseLimitHandler;
|
||||
|
||||
private HttpConnector connector = HttpConnector.DEFAULT;
|
||||
|
||||
@@ -123,7 +123,7 @@ public class GitHub {
|
||||
* @param connector
|
||||
* HttpConnector to use. Pass null to use default connector.
|
||||
*/
|
||||
/* package */ GitHub(String apiUrl, String login, String oauthAccessToken, String password, HttpConnector connector, RateLimitHandler rateLimitHandler, RateLimitHandler abuseLimitHandler) throws IOException {
|
||||
/* package */ GitHub(String apiUrl, String login, String oauthAccessToken, String password, HttpConnector connector, RateLimitHandler rateLimitHandler, AbuseLimitHandler abuseLimitHandler) throws IOException {
|
||||
if (apiUrl.endsWith("/")) apiUrl = apiUrl.substring(0, apiUrl.length()-1); // normalize
|
||||
this.apiUrl = apiUrl;
|
||||
if (null != connector) this.connector = connector;
|
||||
|
||||
@@ -30,7 +30,7 @@ public class GitHubBuilder {
|
||||
private HttpConnector connector;
|
||||
|
||||
private RateLimitHandler rateLimitHandler = RateLimitHandler.WAIT;
|
||||
private RateLimitHandler abuseLimitHandler = RateLimitHandler.ABUSE;
|
||||
private AbuseLimitHandler abuseLimitHandler = AbuseLimitHandler.WAIT;
|
||||
|
||||
public GitHubBuilder() {
|
||||
}
|
||||
@@ -179,6 +179,10 @@ public class GitHubBuilder {
|
||||
this.rateLimitHandler = handler;
|
||||
return this;
|
||||
}
|
||||
public GitHubBuilder withAbuseLimitHandler(AbuseLimitHandler handler) {
|
||||
this.abuseLimitHandler = handler;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures {@linkplain #withConnector(HttpConnector) connector}
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.net.HttpURLConnection;
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
* @see GitHubBuilder#withRateLimitHandler(RateLimitHandler)
|
||||
* @see AbuseLimitHandler
|
||||
*/
|
||||
public abstract class RateLimitHandler {
|
||||
/**
|
||||
@@ -48,27 +49,6 @@ public abstract class RateLimitHandler {
|
||||
return Math.max(10000, Long.parseLong(v)*1000 - System.currentTimeMillis());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Wait until the API abuse "wait time" is passed.
|
||||
*/
|
||||
public static final RateLimitHandler ABUSE = new RateLimitHandler() {
|
||||
@Override
|
||||
public void onError(IOException e, HttpURLConnection uc) throws IOException {
|
||||
try {
|
||||
Thread.sleep(parseWaitTime(uc));
|
||||
} catch (InterruptedException _) {
|
||||
throw (InterruptedIOException)new InterruptedIOException().initCause(e);
|
||||
}
|
||||
}
|
||||
|
||||
private long parseWaitTime(HttpURLConnection uc) {
|
||||
String v = uc.getHeaderField("Retry-After");
|
||||
if (v==null) return 60 * 1000; // can't tell, return 1 min
|
||||
|
||||
return Math.max(1000, Long.parseLong(v)*1000);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Fail immediately.
|
||||
|
||||
@@ -572,10 +572,9 @@ class Requester {
|
||||
root.rateLimitHandler.onError(e,uc);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check to see whether we hit a 403, and the Retry-After field is
|
||||
// available.
|
||||
if (responseCode == HttpURLConnection.HTTP_FORBIDDEN &&
|
||||
|
||||
// Retry-After is not documented but apparently that field exists
|
||||
if (responseCode == HttpURLConnection.HTTP_FORBIDDEN &&
|
||||
uc.getHeaderField("Retry-After") != null) {
|
||||
this.root.abuseLimitHandler.onError(e,uc);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user