Class BotLoopGuard
java.lang.Object
io.agentscope.extensions.channel.common.BotLoopGuard
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
ConstructorsConstructorDescriptionBotLoopGuard(int maxEventsPerWindow, long windowMillis, long cooldownMillis) -
Method Summary
Modifier and TypeMethodDescriptionbooleanRecords an event forpeerKeyand returnstruewhen the peer is within budget (caller may proceed).intRemoves peers that have recorded no event for longer thanwindowMillis + 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.booleanisCoolingDown(String peerKey) ReturnstruewhenpeerKeyis currently in cooldown.intReturns the number of currently tracked peers; mostly for tests/observability.
-
Constructor Details
-
BotLoopGuard
public BotLoopGuard() -
BotLoopGuard
public BotLoopGuard(int maxEventsPerWindow, long windowMillis, long cooldownMillis)
-
-
Method Details
-
allow
Records an event forpeerKeyand returnstruewhen the peer is within budget (caller may proceed). When the per-window cap is exceeded, the peer enters cooldown and this method returnsfalseuntil the cooldown elapses. -
isCoolingDown
ReturnstruewhenpeerKeyis 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 thanwindowMillis + 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
-