Class TokenBucket

java.lang.Object
org.gridgain.internal.mcp.ratelimit.TokenBucket

public class TokenBucket extends Object
Thread-safe token bucket rate limiter. Tokens refill continuously at a constant rate.
  • Constructor Details

    • TokenBucket

      public TokenBucket(long capacity, long refillPeriodMs)
      Creates a token bucket.
      Parameters:
      capacity - Maximum tokens (e.g., 100)
      refillPeriodMs - Time to refill all tokens (e.g., 60000 for per-minute)
  • Method Details

    • tryConsume

      public boolean tryConsume()
      Attempts to consume one token.
      Returns:
      true if token consumed, false if rate limit exceeded
    • getAvailableTokens

      public int getAvailableTokens()
      Gets available tokens without consuming.
      Returns:
      number of available tokens
    • getTimeUntilNextToken

      public long getTimeUntilNextToken()
      Gets time until next token is available.
      Returns:
      milliseconds until next token, or 0 if tokens available
    • getCapacity

      public long getCapacity()
      Gets the capacity of this bucket.