Class GithubRateLimitedAsyncClient

java.lang.Object
io.github.jeremylong.openvulnerability.client.ghsa.GithubRateLimitedAsyncClient
All Implemented Interfaces:
Closeable, AutoCloseable

public final class GithubRateLimitedAsyncClient extends Object implements Closeable
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)
All retries are best-effort and you should still handle failures at the caller.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Semaphore
     
    private final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient
     
    private final Object
     
    private static final org.slf4j.Logger
    Reference to the logger.
    private final int
     
    private final long
     
    private long
     
    private long
     
    private long
     
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    GithubRateLimitedAsyncClient(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 Type
    Method
    Description
    void
     
    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.
    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)
     
    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.
    private String
    extractMessage(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response)
     
    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)
     
    private boolean
    isRateLimited(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response, int status)
    Determines if a response indicates rate limiting.
    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)
     
    private void
    updatePrimaryRateLimitFromHeaders(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOG

      private static final org.slf4j.Logger LOG
      Reference to the logger.
    • delegate

      private final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient delegate
    • scheduler

      private final ScheduledExecutorService scheduler
    • concurrency

      private final Semaphore concurrency
    • maxSecondaryRetries

      private final int maxSecondaryRetries
    • minDelayBetweenRequestsMs

      private final long minDelayBetweenRequestsMs
    • lock

      private final Object 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 client
      maxConcurrentRequests - Max concurrent requests to allow
      maxRequestsPerMinute - Max requests per minute to allow
      maxSecondaryRetries - 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 fresh SimpleHttpRequest for each attempt
      Returns:
      a CompletableFuture that 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 response
      status - 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 response
      attempt - the current retry attempt number
      Returns:
      the backoff delay in milliseconds
    • extractMessage

      private String extractMessage(org.apache.hc.client5.http.async.methods.SimpleHttpResponse response)
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException