Class BotLoopGuard

java.lang.Object
io.agentscope.extensions.channel.common.BotLoopGuard

public final class BotLoopGuard extends Object
Per-peer sliding-window throttle for inbound channel events. Protects the bot from being looped into runaway interaction with another bot (or a stuck user/script).

Defaults follow OpenClaw: 20 events / 60s window. Once tripped the peer is placed in a 60s cooldown during which allow(String) returns false.

Tracked peers do not accumulate forever: a peer with no recorded event for longer than windowMillis + cooldownMillis is evicted (by then its sliding window is empty and any cooldown has expired, so the entry carries no information). Sweeping is opportunistic — no background thread is started: the first allow(String) call after each windowMillis + cooldownMillis period sweeps idle peers, so a peer is reclaimed within roughly one further period after going idle. evictIdlePeers() offers the same sweep explicitly. This class is thread-safe.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
    BotLoopGuard(int maxEventsPerWindow, long windowMillis, long cooldownMillis)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    allow(String peerKey)
    Records an event for peerKey and returns true when the peer is within budget (caller may proceed).
    int
    Removes peers that have recorded no event for longer than windowMillis + cooldownMillis; by then the sliding window is empty and any cooldown has expired, so an evicted peer simply restarts with a fresh window on its next event.
    boolean
    Returns true when peerKey is currently in cooldown.
    int
    Returns the number of currently tracked peers; mostly for tests/observability.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BotLoopGuard

      public BotLoopGuard()
    • BotLoopGuard

      public BotLoopGuard(int maxEventsPerWindow, long windowMillis, long cooldownMillis)
  • Method Details

    • allow

      public boolean allow(String peerKey)
      Records an event for peerKey and returns true when the peer is within budget (caller may proceed). When the per-window cap is exceeded, the peer enters cooldown and this method returns false until the cooldown elapses.
    • isCoolingDown

      public boolean isCoolingDown(String peerKey)
      Returns true when peerKey is currently in cooldown.
    • trackedPeers

      public int trackedPeers()
      Returns the number of currently tracked peers; mostly for tests/observability.
    • evictIdlePeers

      public int evictIdlePeers()
      Removes peers that have recorded no event for longer than windowMillis + cooldownMillis; by then the sliding window is empty and any cooldown has expired, so an evicted peer simply restarts with a fresh window on its next event. A newly created entry carries its creation time as its last-event stamp, so it can never be mistaken for idle here.
      Returns:
      the number of peers removed