Class TokenBucket
java.lang.Object
org.gridgain.internal.mcp.ratelimit.TokenBucket
Thread-safe token bucket rate limiter.
Tokens refill continuously at a constant rate.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintGets available tokens without consuming.longGets the capacity of this bucket.longGets time until next token is available.booleanAttempts to consume one token.
-
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.
-