Class RateLimitedClient

java.lang.Object
io.github.jeremylong.openvulnerability.client.nvd.RateLimitedClient
All Implemented Interfaces:
AutoCloseable

class RateLimitedClient extends Object implements AutoCloseable
Rate limited client for use with a rate limited API. The client contains two mechanisms to control the rate:
  1. RateMeter - limits the number of calls over a given time period
  2. Delay - the minimum delay between calls

The two mechanisms may appear redundant - but each have its purpose in making the calls as fast as can be done while being kind to the endpoint. If one is calling an endpoint, such as the NVD vulnerability API which is limited to 5 calls in 30 seconds without an API Key, to retrieve 4 page of data as quickly as possible you could set a smaller delay and still keep the Rate Meter to limit to 5 calls per 30 seconds. However, if you are retrieving a large number of pages you would want the delay to be slightly under the time period divided by the allowed number of calls (e.g., if we allowed 5 calls over 30 seconds we would use 30/5=6 seconds).

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
     
    private static class 
     
    private static class 
     
    (package private) static class 
    Future response.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) ForcedDiskCacheStorage
     
    private final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient
    The underlying Async Client.
    private long
    The minimum delay in milliseconds between API calls.
    private final ExecutorService
    Executor service for async implementation.
    private long
    The epoch time of the last request.
    private static final org.slf4j.Logger
    Reference to the logger.
    private final RateMeter
    Rate limiting meter.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a rate limited client without the default minimum delay and 5 requests over a 30-second window.
    RateLimitedClient(boolean hasApiKey)
    Construct a rate limited client without the default minimum delay and 5 requests over a 30-second window without an API key or 50 requests over a 30-second window with an API Key..
    RateLimitedClient(int requestsPerThirtySeconds)
    Construct a rate limited client.
    RateLimitedClient(int maxRetries, long minimumDelay, int requestsPerThirtySeconds, HttpAsyncClientSupplier httpClientSupplier, boolean hasApiKey)
    Construct a rate limited client with a given delay and request window configuration.
    RateLimitedClient(int maxRetries, long minimumDelay, int requestsPerThirtySeconds, HttpAsyncClientSupplier httpClientSupplier, Path cachePath, boolean hasApiKey)
    Construct a rate limited client with a given delay and request window configuration.
    RateLimitedClient(long delay, int requestsPerThirtySeconds, boolean hasApiKey)
    Construct a rate limited client with a given delay and request window configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    delayedExecute(org.apache.hc.client5.http.async.methods.SimpleHttpRequest request, int startIndex)
    Ensures the minimum delay has passed since the last call and calls the API.
    (package private) Future<RateLimitedCall>
    execute(org.apache.hc.client5.http.async.methods.SimpleHttpRequest request, int startIndex)
    Ensures the minimum delay has passed since the last call and asynchronously calls the API.
    long
    Returns the delay between API calls in milliseconds.
    (package private) void
    setDelay(long milliseconds)
    Sets the minimum delay between API calls.

    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.
    • client

      private final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient client
      The underlying Async Client.
    • executor

      private final ExecutorService executor
      Executor service for async implementation.
    • lastRequest

      private long lastRequest
      The epoch time of the last request.
    • delay

      private long delay
      The minimum delay in milliseconds between API calls.
    • meter

      private final RateMeter meter
      Rate limiting meter.
    • cacheStorage

  • Constructor Details

    • RateLimitedClient

      RateLimitedClient()
      Construct a rate limited client without the default minimum delay and 5 requests over a 30-second window.
    • RateLimitedClient

      RateLimitedClient(boolean hasApiKey)
      Construct a rate limited client without the default minimum delay and 5 requests over a 30-second window without an API key or 50 requests over a 30-second window with an API Key..
    • RateLimitedClient

      RateLimitedClient(int requestsPerThirtySeconds)
      Construct a rate limited client.
      Parameters:
      requestsPerThirtySeconds - the number of requests allowed over a rolling 30-second window.
    • RateLimitedClient

      RateLimitedClient(long delay, int requestsPerThirtySeconds, boolean hasApiKey)
      Construct a rate limited client with a given delay and request window configuration. This allows callers to configure 5 requests are allowed over a 30-second rolling window and we will delay at least 4 seconds between calls to help more evenly distribute the calls across the request window.
      Parameters:
      delay - the number of milliseconds to wait between API calls
      requestsPerThirtySeconds - the number of requests allowed over a rolling 30-second window
    • RateLimitedClient

      RateLimitedClient(int maxRetries, long minimumDelay, int requestsPerThirtySeconds, HttpAsyncClientSupplier httpClientSupplier, boolean hasApiKey)
      Construct a rate limited client with a given delay and request window configuration. This allows callers to configure 5 requests are allowed over a 30-second rolling window and we will delay at least 4 seconds between calls to help more evenly distribute the calls across the request window.
      Parameters:
      maxRetries - the maximum number of retry attempts
      minimumDelay - the number of milliseconds to wait between API calls
      requestsPerThirtySeconds - the number of requests allowed over a rolling 30-second window
      httpClientSupplier - supplier for custom HTTP clients; if null a default client will be used
    • RateLimitedClient

      RateLimitedClient(int maxRetries, long minimumDelay, int requestsPerThirtySeconds, HttpAsyncClientSupplier httpClientSupplier, Path cachePath, boolean hasApiKey)
      Construct a rate limited client with a given delay and request window configuration. This allows callers to configure 5 requests are allowed over a 30-second rolling window and we will delay at least 4 seconds between calls to help more evenly distribute the calls across the request window.
      Parameters:
      maxRetries - the maximum number of retry attempts
      minimumDelay - the number of milliseconds to wait between API calls
      requestsPerThirtySeconds - the number of requests allowed over a rolling 30-second window
      httpClientSupplier - supplier for custom HTTP clients; if null a default client will be used
      cachePath - the path to the cache directory, can be null
  • Method Details

    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception
    • getDelay

      public long getDelay()
      Returns the delay between API calls in milliseconds.
      Returns:
      the delay between API calls in milliseconds
    • setDelay

      void setDelay(long milliseconds)
      Sets the minimum delay between API calls.
      Parameters:
      milliseconds - the delay duration
    • execute

      Future<RateLimitedCall> execute(org.apache.hc.client5.http.async.methods.SimpleHttpRequest request, int startIndex)
      Ensures the minimum delay has passed since the last call and asynchronously calls the API.
      Parameters:
      request - the request
      Returns:
      the future response
    • delayedExecute

      private RateLimitedCall delayedExecute(org.apache.hc.client5.http.async.methods.SimpleHttpRequest request, int startIndex) throws ExecutionException, InterruptedException
      Ensures the minimum delay has passed since the last call and calls the API.
      Parameters:
      request - the request
      Returns:
      the future response
      Throws:
      ExecutionException - thrown if there is an exception
      InterruptedException - thrown if interrupted