Enum Class LimitStrategy

java.lang.Object
java.lang.Enum<LimitStrategy>
dev.relism.flash.ext.limiter.LimitStrategy
All Implemented Interfaces:
Serializable, Comparable<LimitStrategy>, Constable

public enum LimitStrategy extends Enum<LimitStrategy>
Enumeration of built-in rate-limit algorithms. Each constant is a factory for its corresponding RateLimitStrategy implementation.

New algorithms can be added here without touching the rest of the extension. The enum value is referenced by Limit.strategy() so user code refers to the algorithm by name (LimitStrategy.FIXED_WINDOW) rather than instantiating strategy objects directly.


 @Limit(key = "ip", requests = 100, window = 1, strategy = LimitStrategy.TOKEN_BUCKET)
 public class SearchHandler extends RequestHandler { ... }
 
  • Enum Constant Details

    • FIXED_WINDOW

      public static final LimitStrategy FIXED_WINDOW
      Fixed-window counter: resets to zero at each clock-aligned window boundary. Simple, minimal memory, but allows up to 2× the limit in bursts that straddle two windows.
    • TOKEN_BUCKET

      public static final LimitStrategy TOKEN_BUCKET
      Token-bucket: tokens refill continuously. Smooth burst absorption — a client that was idle accumulates tokens and can fire a short burst, but sustained excess traffic is rejected. Preferred for API endpoints where occasional bursts are legitimate.
    • SLIDING_WINDOW

      public static final LimitStrategy SLIDING_WINDOW
      Sliding-window counter: interpolates between the previous window's count and the current window's count weighted by how far into the current window we are. Eliminates the boundary burst of FIXED_WINDOW while remaining O(1) memory and lock-free. Slight approximation — worst-case error ≈ a few percent at window boundaries.
  • Method Details

    • values

      public static LimitStrategy[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static LimitStrategy valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • create

      public abstract RateLimitStrategy create()
      Creates a fresh, stateless RateLimitStrategy instance for this algorithm.