Package com.gmt2001.ratelimiters
Class WindowedSwitchingRateLimiter
java.lang.Object
com.gmt2001.ratelimiters.WindowedRateLimiter
com.gmt2001.ratelimiters.WindowedSwitchingRateLimiter
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
-
Field Summary
Fields inherited from class com.gmt2001.ratelimiters.WindowedRateLimiter
currentTokens, limit, mutex, nextReset, windowMS
-
Constructor Summary
ConstructorsConstructorDescriptionWindowedSwitchingRateLimiter
(long windowMS, int mainLimit, int alternateLimit) ConstructorWindowedSwitchingRateLimiter
(long windowMS, int mainLimit, int alternateLimit, boolean isMain) ConstructorWindowedSwitchingRateLimiter
(Duration window, int mainLimit, int alternateLimit) ConstructorWindowedSwitchingRateLimiter
(Duration window, int mainLimit, int alternateLimit, boolean isMain) Constructor -
Method Summary
Modifier and TypeMethodDescriptionint
boolean
int
limit()
int
void
switchLimit
(boolean isMain) Switches which limit is activeMethods inherited from class com.gmt2001.ratelimiters.WindowedRateLimiter
currentTokens, isTokenAvailable, nextReset, reset, takeToken, waitAndRun, waitAndTakeToken, windowMS
-
Constructor Details
-
WindowedSwitchingRateLimiter
Constructor- Parameters:
windowMS
- The length of the windowmainLimit
- The normal maximum number of tokens available during the windowalternateLimit
- 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 windowmainLimit
- The normal maximum number of tokens available during the windowalternateLimit
- The alternate maximum number of tokens available during the windowisMain
-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 millisecondsmainLimit
- The normal maximum number of tokens available during the windowalternateLimit
- 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 millisecondsmainLimit
- The normal maximum number of tokens available during the windowalternateLimit
- The alternate maximum number of tokens available during the windowisMain
-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 classWindowedRateLimiter
- 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
-