Class RateMeter
java.lang.Object
io.github.jeremylong.openvulnerability.client.nvd.RateMeter
A rate meter to limit the number of calls over a rolling time window.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final longThe duration of the rolling window in milliseconds.private final ReentrantLockLock to ensure thread safety.private static final org.slf4j.LoggerReference to the logger.private final intThe maximum number of operations allowed in the rolling window.Queue to track timestamps of allowed operations. -
Constructor Summary
ConstructorsConstructorDescriptionRateMeter(int quantity, long durationMilliseconds) Constructs a new rate meter with the specified quantity and duration. -
Method Summary
Modifier and TypeMethodDescriptionprivate longCalculates the wait time needed before the next request can be made.voidChecks if the rate limit has been reached and waits if necessary.longReturns the duration of the rolling window in milliseconds.intReturns the quantity of operations allowed in the rolling window.private voidRecords a new request in the rate meter.
-
Field Details
-
LOG
private static final org.slf4j.Logger LOGReference to the logger. -
quantity
private final int quantityThe maximum number of operations allowed in the rolling window. -
durationMilliseconds
private final long durationMillisecondsThe duration of the rolling window in milliseconds. -
ticketTimestamps
Queue to track timestamps of allowed operations. -
lock
Lock to ensure thread safety.
-
-
Constructor Details
-
RateMeter
public RateMeter(int quantity, long durationMilliseconds) Constructs a new rate meter with the specified quantity and duration.- Parameters:
quantity- the maximum number of operations allowed in the rolling windowdurationMilliseconds- the duration of the rolling window in milliseconds
-
-
Method Details
-
checkRateLimit
Checks if the rate limit has been reached and waits if necessary.- Throws:
InterruptedException- if the thread is interrupted while waiting
-
calculateWaitTime
private long calculateWaitTime()Calculates the wait time needed before the next request can be made.- Returns:
- the wait time in milliseconds, or 0 if no wait is needed
-
recordRequest
private void recordRequest()Records a new request in the rate meter. -
getQuantity
public int getQuantity()Returns the quantity of operations allowed in the rolling window.- Returns:
- the quantity of operations allowed in the rolling window.
-
getDuration
public long getDuration()Returns the duration of the rolling window in milliseconds.- Returns:
- the duration of the rolling window in milliseconds.
-