Class ApiRateLimiter

java.lang.Object
com.ultikits.ultitools.utils.ApiRateLimiter

public class ApiRateLimiter extends Object
Simple rate limiter for UltiCloud API calls. Prevents excessive API calls during crash loops or rapid restarts.
UltiCloud API调用的简单速率限制器。 防止崩溃循环或快速重启期间的过多API调用。
  • Field Details

    • lastCallTimestamps

      private static final Map<String,Long> 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

      public static boolean isAllowed(String key, long cooldownMs)
      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

      public static boolean isAllowed(String key)
      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

      public static long getRemainingCooldown(String key, long cooldownMs)
      Get remaining cooldown time in seconds for a given key.
      Parameters:
      key - the API call type key
      cooldownMs - the cooldown period in milliseconds
      Returns:
      remaining seconds, or 0 if no cooldown active
    • reset

      public static void reset(String key)
      Reset the rate limiter for a specific key (e.g., after successful login).
    • resetAll

      public static void resetAll()
      Reset all rate limits.