java.lang.Object
io.github.jeremylong.openvulnerability.client.nvd.RateMeter

public class RateMeter extends Object
A rate meter to limit the number of calls over a rolling time window.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final long
    The duration of the rolling window in milliseconds.
    private final ReentrantLock
    Lock to ensure thread safety.
    private static final org.slf4j.Logger
    Reference to the logger.
    private final int
    The maximum number of operations allowed in the rolling window.
    private final Queue<Long>
    Queue to track timestamps of allowed operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RateMeter(int quantity, long durationMilliseconds)
    Constructs a new rate meter with the specified quantity and duration.
  • Method Summary

    Modifier and Type
    Method
    Description
    private long
    Calculates the wait time needed before the next request can be made.
    void
    Checks if the rate limit has been reached and waits if necessary.
    long
    Returns the duration of the rolling window in milliseconds.
    int
    Returns the quantity of operations allowed in the rolling window.
    private void
    Records a new request in the rate meter.

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

      private final int quantity
      The maximum number of operations allowed in the rolling window.
    • durationMilliseconds

      private final long durationMilliseconds
      The duration of the rolling window in milliseconds.
    • ticketTimestamps

      private final Queue<Long> ticketTimestamps
      Queue to track timestamps of allowed operations.
    • lock

      private final ReentrantLock 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 window
      durationMilliseconds - the duration of the rolling window in milliseconds
  • Method Details

    • checkRateLimit

      public void checkRateLimit() throws InterruptedException
      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.