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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final long
     
    private static final Map<String,Long>
     
    private static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    getRemainingCooldown(String key, long cooldownMs)
    Get remaining cooldown time in seconds for a given key.
    static boolean
    Check if an API call is allowed using the default cooldown.
    static boolean
    isAllowed(String key, long cooldownMs)
    Check if an API call is allowed.
    static boolean
    Check if a login attempt is allowed (uses longer cooldown).
    (package private) static long
    remainingSeconds(long lastCall, long cooldownMs, long now)
    Whole seconds left of a cooldown, rounded up: 9.9 s left is shown as 10, and any part of a second left as 1, so a caller that is still refused never reads "0 seconds" (#753).
    static void
    Reset the rate limiter for a specific key (e.g., after successful login).
    static void
    Reset all rate limits.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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, rounded up, or 0 if no cooldown active
    • remainingSeconds

      static long remainingSeconds(long lastCall, long cooldownMs, long now)
      Whole seconds left of a cooldown, rounded up: 9.9 s left is shown as 10, and any part of a second left as 1, so a caller that is still refused never reads "0 seconds" (#753).
      Parameters:
      lastCall - when the cooldown started, in epoch milliseconds
      cooldownMs - the cooldown period in milliseconds
      now - the current time in epoch milliseconds
      Returns:
      the remaining seconds rounded up, or 0 once the cooldown has passed
    • 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.