Class TokenRateLimiter
- java.lang.Object
-
- org.openidentityplatform.openig.ai.filter.TokenRateLimiter
-
public class TokenRateLimiter extends Object
Identity-aware, in-memory token-bucket rate limiter for LLM prompt tokens.Configuration
"rate": { "numberOfTokens": 10000, "duration": "1 minute" }
-
-
Constructor Summary
Constructors Constructor Description TokenRateLimiter(long numberOfTokens, org.forgerock.util.time.Duration duration)Minimal constructor, with system ticker and no bucket evictionTokenRateLimiter(long numberOfTokens, org.forgerock.util.time.Duration duration, com.github.benmanes.caffeine.cache.Ticker ticker, org.forgerock.util.time.Duration cleaningInterval)Full constructor, matching the pattern ofTokenBucketThrottlingStrategy(Ticker, ScheduledExecutorService, Duration).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longavailableTokens(String sub)voidstop()longtryConsume(String sub, long cost)Try to consume N tokens (e.g. costly endpoints) foridentity
-
-
-
Constructor Detail
-
TokenRateLimiter
public TokenRateLimiter(long numberOfTokens, org.forgerock.util.time.Duration duration)Minimal constructor, with system ticker and no bucket eviction- Parameters:
numberOfTokens- maximum tokens per window (= burst capacity)duration- window size — OpenIGDurationsyntax
-
TokenRateLimiter
public TokenRateLimiter(long numberOfTokens, org.forgerock.util.time.Duration duration, com.github.benmanes.caffeine.cache.Ticker ticker, org.forgerock.util.time.Duration cleaningInterval)Full constructor, matching the pattern ofTokenBucketThrottlingStrategy(Ticker, ScheduledExecutorService, Duration).When
cleaningIntervalis non-null, a periodic eviction task removes buckets that have been idle for at least one full window, preventing unbounded heap growth in long-running deployments.- Parameters:
numberOfTokens- maximum tokens per window (= burst capacity)duration- window size — OpenIGDurationsyntaxticker- monotonic time source; useTicker.systemTicker()in production andFakeTickerin testscleaningInterval- eviction period; ignored when executor isnull
-
-