Class ThrottleFilter

java.lang.Object
cloud.opencode.base.log.filter.ThrottleFilter
All Implemented Interfaces:
LogFilter

public final class ThrottleFilter extends Object implements LogFilter
Throttle Filter - Rate-Limits Duplicate Log Messages 节流过滤器 - 限制重复日志消息的速率

Denies log events with the same message if they were logged within the configured time interval. Uses monotonic System.nanoTime() for accurate timing.

如果相同消息在配置的时间间隔内已被记录,则拒绝日志事件。 使用单调的 System.nanoTime() 进行精确计时。

Features | 主要功能:

  • Per-message rate limiting - 每消息速率限制
  • Monotonic timing via nanoTime - 通过 nanoTime 的单调计时
  • Cache size limit of 10000 entries with oldest eviction - 10000 条目的缓存大小限制,淘汰最旧条目
  • Thread-safe via ConcurrentHashMap - 通过 ConcurrentHashMap 实现线程安全

Usage Examples | 使用示例:

// Throttle duplicate messages within 5 seconds
ThrottleFilter filter = new ThrottleFilter(Duration.ofSeconds(5));
chain.addFilter(filter);

// Clear the throttle cache
filter.clearCache();

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap) - 线程安全: 是(ConcurrentHashMap)
Since:
JDK 25, opencode-base-log V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ThrottleFilter

      public ThrottleFilter(Duration interval)
      Creates a throttle filter with the specified interval. 使用指定间隔创建节流过滤器。
      Parameters:
      interval - the minimum interval between duplicate messages | 重复消息之间的最小间隔
      Throws:
      NullPointerException - if interval is null | 如果间隔为 null
      IllegalArgumentException - if interval is negative or zero | 如果间隔为负数或零
  • Method Details

    • filter

      public FilterAction filter(LogEvent event)
      Filters the event based on message throttling. 根据消息节流过滤事件。
      Specified by:
      filter in interface LogFilter
      Parameters:
      event - the log event | 日志事件
      Returns:
      DENY if the same message was logged within the interval, NEUTRAL otherwise | 如果相同消息在间隔内已被记录返回 DENY,否则返回 NEUTRAL
    • clearCache

      public void clearCache()
      Clears the throttle cache. 清除节流缓存。