Class 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 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 — OpenIG Duration syntax
      • 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 of TokenBucketThrottlingStrategy(Ticker, ScheduledExecutorService, Duration).

        When cleaningInterval is 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 — OpenIG Duration syntax
        ticker - monotonic time source; use Ticker.systemTicker() in production and FakeTicker in tests
        cleaningInterval - eviction period; ignored when executor is null
    • Method Detail

      • tryConsume

        public long tryConsume​(String sub,
                               long cost)
        Try to consume N tokens (e.g. costly endpoints) for identity
      • availableTokens

        public long availableTokens​(String sub)
      • stop

        public void stop()