Package com.ultikits.ultitools.utils
Class ApiRateLimiter
java.lang.Object
com.ultikits.ultitools.utils.ApiRateLimiter
Simple rate limiter for UltiCloud API calls.
Prevents excessive API calls during crash loops or rapid restarts.
UltiCloud API调用的简单速率限制器。 防止崩溃循环或快速重启期间的过多API调用。
UltiCloud API调用的简单速率限制器。 防止崩溃循环或快速重启期间的过多API调用。
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final longprivate static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longgetRemainingCooldown(String key, long cooldownMs) Get remaining cooldown time in seconds for a given key.static booleanCheck if an API call is allowed using the default cooldown.static booleanCheck if an API call is allowed.static booleanCheck if a login attempt is allowed (uses longer cooldown).static voidReset the rate limiter for a specific key (e.g., after successful login).static voidresetAll()Reset all rate limits.
-
Field Details
-
lastCallTimestamps
-
DEFAULT_COOLDOWN_MS
private static final long DEFAULT_COOLDOWN_MS- See Also:
-
LOGIN_COOLDOWN_MS
private static final long LOGIN_COOLDOWN_MS- See Also:
-
-
Constructor Details
-
ApiRateLimiter
public ApiRateLimiter()
-
-
Method Details
-
isAllowed
Check if an API call is allowed. Returns true if enough time has passed since the last call with the same key.- Parameters:
key- unique identifier for the API call type (e.g., "login", "register")cooldownMs- minimum time between calls in milliseconds- Returns:
- true if the call is allowed, false if rate limited
-
isAllowed
Check if an API call is allowed using the default cooldown. -
isLoginAllowed
public static boolean isLoginAllowed()Check if a login attempt is allowed (uses longer cooldown). -
getRemainingCooldown
Get remaining cooldown time in seconds for a given key.- Parameters:
key- the API call type keycooldownMs- the cooldown period in milliseconds- Returns:
- remaining seconds, or 0 if no cooldown active
-
reset
Reset the rate limiter for a specific key (e.g., after successful login). -
resetAll
public static void resetAll()Reset all rate limits.
-