Class WindowedSwitchingRateLimiter

java.lang.Object
com.gmt2001.ratelimiters.WindowedRateLimiter
com.gmt2001.ratelimiters.WindowedSwitchingRateLimiter

public class WindowedSwitchingRateLimiter extends WindowedRateLimiter
Handles rate limiting using a window which resets a period of time after the first token is used, and where the limit can switch between two values
Author:
gmt2001
  • Constructor Details

    • WindowedSwitchingRateLimiter

      public WindowedSwitchingRateLimiter(Duration window, int mainLimit, int alternateLimit)
      Constructor
      Parameters:
      windowMS - The length of the window
      mainLimit - The normal maximum number of tokens available during the window
      alternateLimit - The alternate maximum number of tokens available during the window
    • WindowedSwitchingRateLimiter

      public WindowedSwitchingRateLimiter(Duration window, int mainLimit, int alternateLimit, boolean isMain)
      Constructor
      Parameters:
      windowMS - The length of the window
      mainLimit - The normal maximum number of tokens available during the window
      alternateLimit - The alternate maximum number of tokens available during the window
      isMain - true to select mainLimit; false to select alternateLimit
    • WindowedSwitchingRateLimiter

      public WindowedSwitchingRateLimiter(long windowMS, int mainLimit, int alternateLimit)
      Constructor
      Parameters:
      windowMS - The length of the window, in milliseconds
      mainLimit - The normal maximum number of tokens available during the window
      alternateLimit - The alternate maximum number of tokens available during the window
    • WindowedSwitchingRateLimiter

      public WindowedSwitchingRateLimiter(long windowMS, int mainLimit, int alternateLimit, boolean isMain)
      Constructor
      Parameters:
      windowMS - The length of the window, in milliseconds
      mainLimit - The normal maximum number of tokens available during the window
      alternateLimit - The alternate maximum number of tokens available during the window
      isMain - true to select mainLimit; false to select alternateLimit
  • Method Details

    • switchLimit

      public void switchLimit(boolean isMain)
      Switches which limit is active
      Parameters:
      isMain - true to select mainLimit; false to select alternateLimit
    • isMainLimit

      public boolean isMainLimit()
      Returns:
      true if currently using mainLimit; false if currently using alternateLimit
    • limit

      public int limit()
      Overrides:
      limit in class WindowedRateLimiter
      Returns:
      The maximum number of tokens available during the window, based on the currently selected limit
    • mainLimit

      public int mainLimit()
      Returns:
      The mainLimit
    • alternateLimit

      public int alternateLimit()
      Returns:
      The alternateLimit