- Direct Known Subclasses:
RateLimitChecker.LiteralValue
GitHub allots a certain number of requests to each user or application per period of time. The number of requests
remaining and the time when the number will be reset is returned in the response header and can also be requested
using GitHub.getRateLimit()
. The "requests per interval" is referred to as the "rate limit".
GitHub prefers that clients stop before exceeding their rate limit rather than stopping after they exceed it. The
RateLimitChecker
is called before each request to check the rate limit and wait if the checker criteria are
met.
- Author:
- Liam Newman
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
ARateLimitChecker
with a simple number as the limit. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
checkRateLimit
(GHRateLimit.Record rateLimitRecord, long count) Decides whether the current request exceeds the allowed "rate limit" budget.protected final boolean
sleepUntilReset
(GHRateLimit.Record record) Sleep until reset.
-
Field Details
-
NONE
The Constant NONE.
-
-
Constructor Details
-
RateLimitChecker
public RateLimitChecker()Create default RateLimitChecker instance
-
-
Method Details
-
checkRateLimit
protected boolean checkRateLimit(GHRateLimit.Record rateLimitRecord, long count) throws InterruptedException Decides whether the current request exceeds the allowed "rate limit" budget. If this determines the rate limit will be exceeded, this method should sleep for some amount of time and must returntrue
. Implementers are free to choose whatever strategy they prefer for what is considered to exceed the budget and how long to sleep.The caller of this method figures out which
GHRateLimit.Record
applies for the current request and provides it to this method.As long as this method returns
true
it is guaranteed thatGitHubRateLimitChecker
will retrieve updated rate limit information and call this method again withcount
incremented by one. When this checker returnsfalse
, the callingGitHubRateLimitChecker
will let the request continue.Rate limit reset times are only accurate to the second. Trying to sleep to exactly the reset time could result in requests being sent before the new rate limit was available. For this reason, if this method returned
true
at least once for a particular request,GitHubRateLimitChecker
may choose to sleep for some small additional between calls and before letting the request continue.- Parameters:
rateLimitRecord
- the currentGHRateLimit.Record
to check against.count
- the number of times in a row this method has been called for the current request- Returns:
false
if the current request does not exceed the allowed budget,true
if the current request exceeded the budget.- Throws:
InterruptedException
- if the thread is interrupted while sleeping
-
sleepUntilReset
Sleep until reset.- Parameters:
record
- the record- Returns:
- true, if successful
- Throws:
InterruptedException
- the interrupted exception
-