@Internal public class RateLimiter extends Object
This implementation uses a token bucket algorithm to control the rate of operations:
The bucket capacity is determined by the amortization limit parameter, which defines how much "credit" can be accumulated during idle periods. This allows the system to compensate for pauses by temporarily working at a maximum rate, up to the bucket capacity.
Example: With a rate of 100 tokens/sec and 2s amortization limit:
Note: This class is not thread-safe.
| Modifier and Type | Field and Description |
|---|---|
static String |
CONFIG_UNLIMITED |
static RateLimiter |
UNLIMITED
A rate limiter implementation that imposes no limits on operations.
|
| Modifier and Type | Method and Description |
|---|---|
long |
available()
Returns the number of tokens available for processing at the specified time.
|
long |
availableOrWait()
Waits if necessary until at least one token becomes available, then returns the number
of tokens available for immediate processing.
|
void |
consume(long tokens)
Consumes the specified number of tokens from the rate limiter, blocking if necessary
until the tokens become available.
|
boolean |
equals(Object o) |
int |
hashCode() |
boolean |
needWait()
Checks whether the current thread needs to wait to maintain the rate limit.
|
static RateLimiter |
of(double rate)
Creates a rate limiter with the specified rate limit and default amortization limit (1 second).
|
static RateLimiter |
of(double rate,
com.devexperts.util.TimePeriod amortizationPeriod)
Creates a rate limiter with the specified parameters.
|
static RateLimiter |
ofBucket(long bucketSize,
long refillPeriodNanos)
Creates a rate limiter with the specified token bucket parameters.
|
void |
reportConsumed(long tokens)
Updates the rate limiter after tokens have been processed.
|
String |
toString() |
static RateLimiter |
valueOf(String config)
Creates a rate limiter from a configuration string.
|
long |
waitTime()
Returns the number of milliseconds that the caller should wait to maintain the rate limit.
|
public static final String CONFIG_UNLIMITED
public static final RateLimiter UNLIMITED
public static RateLimiter valueOf(String config)
Special cases:
null, empty string, or "UNLIMITED" (case-insensitive) - returns the UNLIMITED
implementation
Configuration format: <rate>[;<bucket>]
<rate> - tokens per second, supports fractional values and unit prefixes (required):
<bucket> - bucket time period or amortization limit (optional, default: 1s). This represents
the maximum accumulated pause that the rate limiter can compensate by working at maximum rate.
Examples:
"100" - 100 tokens/sec, 1s bucket (100 tokens)"0.5;2s" - 0.5 tokens/sec, 2s bucket (one token)"0.5k" - 500 tokens/sec, 1s bucket (500 tokens)"0.5ki" - 512 tokens/sec, 1s bucket (512 tokens)"1M" - 1,000,000 tokens/sec, 1s bucket (1,000,000 tokens)"1Mi" - 1,048,576 tokens/sec, 1s bucket (1,048,576 tokens)"1000;0.5s" - 1,000 tokens/sec, 0.5s bucket (500 tokens)config - the configuration string (may be null)UNLIMITED for null/empty/"UNLIMITED" stringsIllegalArgumentException - if the configuration format is invalidpublic static RateLimiter of(double rate)
rate - the sustained rate limit in tokens per secondIllegalArgumentException - if the rate limit is not positivepublic static RateLimiter of(double rate, com.devexperts.util.TimePeriod amortizationPeriod)
rate - the sustained rate limit in tokens per secondamortizationPeriod - the time period for bucket capacity calculationIllegalArgumentException - if any parameter is not positivepublic static RateLimiter ofBucket(long bucketSize, long refillPeriodNanos)
bucketSize - the maximum number of tokens the bucket can hold (burst capacity)refillPeriodNanos - the time period in nanoseconds for refilling the bucket to its full capacityIllegalArgumentException - if bucketSize or refillPeriodNanos is not positivepublic boolean needWait()
true if the thread needs to wait, false otherwisepublic long waitTime()
public long availableOrWait()
throws InterruptedException
This method blocks if no tokens are currently available, waiting until the rate limiter refills enough to provide at least one token. Once a token is available, it returns the total number of tokens that can be consumed immediately.
InterruptedException - if the current thread is interrupted while waitingpublic long available()
public void consume(long tokens)
throws InterruptedException
tokens - the number of tokens to consume (must be non-negative)IllegalArgumentException - if tokens are negativeInterruptedException - if the current thread is interrupted while waitingpublic void reportConsumed(long tokens)
tokens - the number of tokens that were processed (must be non-negative)IllegalArgumentException - if tokens are negativeCopyright © 2002–2025 Devexperts LLC. All rights reserved.