Enum Class RateLimitAlgorithm

java.lang.Object
java.lang.Enum<RateLimitAlgorithm>
tech.riemann.nif.web.ratelimit.RateLimitAlgorithm
All Implemented Interfaces:
Serializable, Comparable<RateLimitAlgorithm>, Constable

public enum RateLimitAlgorithm extends Enum<RateLimitAlgorithm>
限流算法枚举

定义支持的限流算法类型

算法对比: 1. TOKEN_BUCKET:令牌桶算法,允许突发流量,适合API限流 2. SLIDING_WINDOW:滑动窗口算法,平滑限流,适合精确控制 3. FIXED_WINDOW:固定窗口计数器,简单高效,适合粗粒度限流

Author:
Kerbores(kerbores@riemann.tech)
  • Enum Constant Details

    • TOKEN_BUCKET

      public static final RateLimitAlgorithm TOKEN_BUCKET
      令牌桶算法

      核心思想: 1. 系统以恒定速率生成令牌 2. 请求需要获取令牌才能执行 3. 桶满时丢弃新令牌 4. 允许一定程度的突发流量

      适用场景:API接口限流,允许短时间内的突发请求

    • SLIDING_WINDOW

      public static final RateLimitAlgorithm SLIDING_WINDOW
      滑动窗口算法

      核心思想: 1. 将时间划分为多个小窗口 2. 统计最近N个窗口内的请求数 3. 超过阈值则拒绝请求

      适用场景:需要精确控制请求速率的场景

    • FIXED_WINDOW

      public static final RateLimitAlgorithm FIXED_WINDOW
      固定窗口计数器

      核心思想: 1. 在固定时间窗口内计数 2. 超过阈值则拒绝请求 3. 窗口结束后重置计数

      适用场景:简单限流,对精度要求不高的场景

  • Method Details

    • values

      public static RateLimitAlgorithm[] 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 RateLimitAlgorithm 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