Repository navigation
Expand file tree
/
Copy pathGitHubRateLimitHandler.java
More file actions
110 lines (96 loc) · 3.91 KB
/
Copy pathGitHubRateLimitHandler.java
File metadata and controls
110 lines (96 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package org.kohsuke.github;
import org.jetbrains.annotations.NotNull;
import org.kohsuke.github.connector.GitHubConnectorResponse;
import org.kohsuke.github.internal.WaitTimeUtils;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.time.Duration;
import javax.annotation.Nonnull;
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
// TODO: Auto-generated Javadoc
/**
* Pluggable strategy to determine what to do when the API rate limit is reached.
*
* @author Kohsuke Kawaguchi
* @author Liam Newman
* @see GitHubBuilder#withRateLimitHandler(GitHubRateLimitHandler)
* @see GitHubAbuseLimitHandler
*/
public abstract class GitHubRateLimitHandler extends GitHubConnectorResponseErrorHandler {
/**
* Fail immediately.
*/
public static final GitHubRateLimitHandler FAIL = new GitHubRateLimitHandler() {
@Override
public void onError(GitHubConnectorResponse connectorResponse) throws IOException {
throw new HttpException("API rate limit reached",
connectorResponse.statusCode(),
connectorResponse.header("Status"),
connectorResponse.request().url().toString())
.withResponseHeaderFields(connectorResponse.allHeaders());
}
};
/**
* Wait until the API abuse "wait time" is passed.
*/
public static final GitHubRateLimitHandler WAIT = new GitHubRateLimitHandler() {
@Override
public void onError(GitHubConnectorResponse connectorResponse) throws IOException {
try {
Thread.sleep(parseWaitTime(connectorResponse));
} catch (InterruptedException ex) {
throw (InterruptedIOException) new InterruptedIOException().initCause(ex);
}
}
};
/**
* On a wait, even if the response suggests a very short wait, wait for a minimum duration.
*/
private static final int MINIMUM_RATE_LIMIT_RETRY_MILLIS = 1000;
/**
* Create default GitHubRateLimitHandler instance
*/
public GitHubRateLimitHandler() {
}
/**
* Called when the library encounters HTTP error indicating that the API rate limit has been exceeded.
*
* <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.
*
* @param connectorResponse
* Response information for this request.
*
* @throws IOException
* the io exception
* @see <a href="https://developer.github.com/v3/#rate-limiting">API documentation from GitHub</a>
*/
public abstract void onError(@Nonnull GitHubConnectorResponse connectorResponse) throws IOException;
/**
* Checks if is error.
*
* @param connectorResponse
* the connector response
* @return true, if is error
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Override
boolean isError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
return connectorResponse.statusCode() == HTTP_FORBIDDEN
&& "0".equals(connectorResponse.header("X-RateLimit-Remaining"));
}
/*
* Exposed for testability. Given an http response, find the rate limit reset header field and parse it. If no
* header is found, wait for a reasonably amount of time.
*
* The implementation lives in WaitTimeUtils, shared with GitHubAbuseLimitHandler.
*/
long parseWaitTime(GitHubConnectorResponse connectorResponse) {
return WaitTimeUtils.parseRateLimitResetWaitTime(connectorResponse,
Duration.ofMinutes(1).toMillis(), // can't tell, return 1 min
MINIMUM_RATE_LIMIT_RETRY_MILLIS);
}
}