Class RateLimitedClient
java.lang.Object
io.github.jeremylong.openvulnerability.client.nvd.RateLimitedClient
- All Implemented Interfaces:
AutoCloseable
Rate limited client for use with a rate limited API. The client contains two mechanisms to control the rate:
- RateMeter - limits the number of calls over a given time period
- 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 ClassesModifier and TypeClassDescriptionprivate static classprivate static classprivate static class(package private) static classFuture response. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) ForcedDiskCacheStorageprivate final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClientThe underlying Async Client.private longThe minimum delay in milliseconds between API calls.private final ExecutorServiceExecutor service for async implementation.private longThe epoch time of the last request.private static final org.slf4j.LoggerReference to the logger.private final RateMeterRate limiting meter. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct 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 TypeMethodDescriptionvoidclose()private RateLimitedCalldelayedExecute(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.longgetDelay()Returns the delay between API calls in milliseconds.(package private) voidsetDelay(long milliseconds) Sets the minimum delay between API calls.
-
Field Details
-
LOG
private static final org.slf4j.Logger LOGReference to the logger. -
client
private final org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient clientThe underlying Async Client. -
executor
Executor service for async implementation. -
lastRequest
private long lastRequestThe epoch time of the last request. -
delay
private long delayThe minimum delay in milliseconds between API calls. -
meter
Rate limiting meter. -
cacheStorage
ForcedDiskCacheStorage 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 callsrequestsPerThirtySeconds- 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 attemptsminimumDelay- the number of milliseconds to wait between API callsrequestsPerThirtySeconds- the number of requests allowed over a rolling 30-second windowhttpClientSupplier- supplier for custom HTTP clients; ifnulla 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 attemptsminimumDelay- the number of milliseconds to wait between API callsrequestsPerThirtySeconds- the number of requests allowed over a rolling 30-second windowhttpClientSupplier- supplier for custom HTTP clients; ifnulla default client will be usedcachePath- the path to the cache directory, can be null
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- 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 exceptionInterruptedException- thrown if interrupted
-