Class GithubRateLimitedAsyncClient
java.lang.Object
io.github.jeremylong.openvulnerability.client.ghsa.GithubRateLimitedAsyncClient
- All Implemented Interfaces:
Closeable,AutoCloseable
A thin wrapper around CloseableHttpAsyncClient that:
- Limits concurrency (to avoid secondary limits)
- Throttles requests to a maxRequestsPerMinute
- Tracks primary rate limit headers
- Applies GitHub's recommended behavior on 403/429 (incl. secondary limits)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Semaphoreprivate final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClientprivate final Objectprivate static final org.slf4j.LoggerReference to the logger.private final intprivate final longprivate longprivate longprivate longprivate final ScheduledExecutorService -
Constructor Summary
ConstructorsConstructorDescriptionGithubRateLimitedAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient delegate, int maxConcurrentRequests, int maxRequestsPerMinute, int maxSecondaryRetries) Create a new rate-limited GitHub async client. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()private longcomputeSecondaryBackoffMillis(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response, int attempt) Secondary rate limit handling based on GitHub docs: Prefer Retry-After if present. If x-ratelimit-remaining == 0 and reset is present, wait until reset. Else wait at least 60 seconds. Apply simple exponential backoff per attempt.private voiddoExecute(Supplier<org.apache.hc.client5.http.async.methods.SimpleHttpRequest> requestSupplier, CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> future, int attempt) CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse>Execute a GitHub request with rate limiting and retry on 403/429.private StringextractMessage(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response) private voidhandleCompleted(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response, Supplier<org.apache.hc.client5.http.async.methods.SimpleHttpRequest> requestSupplier, CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> future, int attempt) private booleanisRateLimited(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response, int status) Determines if a response indicates rate limiting.private voidscheduleAttempt(Supplier<org.apache.hc.client5.http.async.methods.SimpleHttpRequest> requestSupplier, CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> future, int attempt) private voidupdatePrimaryRateLimitFromHeaders(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response)
-
Field Details
-
LOG
private static final org.slf4j.Logger LOGReference to the logger. -
delegate
private final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient delegate -
scheduler
-
concurrency
-
maxSecondaryRetries
private final int maxSecondaryRetries -
minDelayBetweenRequestsMs
private final long minDelayBetweenRequestsMs -
lock
-
remaining
private long remaining -
resetEpochSeconds
private long resetEpochSeconds -
nextAllowedRequestTimeMs
private long nextAllowedRequestTimeMs
-
-
Constructor Details
-
GithubRateLimitedAsyncClient
public GithubRateLimitedAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient delegate, int maxConcurrentRequests, int maxRequestsPerMinute, int maxSecondaryRetries) Create a new rate-limited GitHub async client.- Parameters:
delegate- The underlying HTTP async clientmaxConcurrentRequests- Max concurrent requests to allowmaxRequestsPerMinute- Max requests per minute to allowmaxSecondaryRetries- Max retries on secondary rate limit errors (403/429)
-
-
Method Details
-
execute
public CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> execute(Supplier<org.apache.hc.client5.http.async.methods.SimpleHttpRequest> requestSupplier) Execute a GitHub request with rate limiting and retry on 403/429.requestSupplier MUST return a fresh SimpleHttpRequest each time it is called. This avoids issues with replaying consumed request bodies.
- Parameters:
requestSupplier- Supplier that creates a freshSimpleHttpRequestfor each attempt- Returns:
- a
CompletableFuturethat completes with the HTTP response or exceptionally on failure
-
scheduleAttempt
private void scheduleAttempt(Supplier<org.apache.hc.client5.http.async.methods.SimpleHttpRequest> requestSupplier, CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> future, int attempt) -
doExecute
private void doExecute(Supplier<org.apache.hc.client5.http.async.methods.SimpleHttpRequest> requestSupplier, CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> future, int attempt) -
handleCompleted
private void handleCompleted(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response, Supplier<org.apache.hc.client5.http.async.methods.SimpleHttpRequest> requestSupplier, CompletableFuture<org.apache.hc.client5.http.async.methods.SimpleHttpResponse> future, int attempt) -
isRateLimited
private boolean isRateLimited(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response, int status) Determines if a response indicates rate limiting. 429 is always rate-limited. 403 is only considered rate-limited if there's evidence (Retry-After header, primary limit exhausted, or message indicates secondary rate limit).- Parameters:
response- the HTTP responsestatus- the HTTP status code- Returns:
- true if the response indicates rate limiting
-
updatePrimaryRateLimitFromHeaders
private void updatePrimaryRateLimitFromHeaders(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response) -
computeSecondaryBackoffMillis
private long computeSecondaryBackoffMillis(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response, int attempt) Secondary rate limit handling based on GitHub docs:- Prefer Retry-After if present.
- If x-ratelimit-remaining == 0 and reset is present, wait until reset.
- Else wait at least 60 seconds.
- Apply simple exponential backoff per attempt.
- Parameters:
response- the HTTP responseattempt- the current retry attempt number- Returns:
- the backoff delay in milliseconds
-
extractMessage
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-