Package org.jivesoftware.util
Class TokenBucketRateLimiter
java.lang.Object
org.jivesoftware.util.TokenBucketRateLimiter
A thread-safe, synchronized token-bucket rate limiter with metrics.
This class limits the rate of arbitrary events. It is not tied to any specific domain such as networking or
connections.
- Author:
- Guus der Kinderen, guus@goodbytes.nl
-
Constructor Summary
ConstructorsConstructorDescriptionTokenBucketRateLimiter(long permitsPerSecond, long maxBurst) Creates a new rate limiter. -
Method Summary
Modifier and TypeMethodDescriptiondoubleReturns the ratio of accepted events to total events.longReturns the total number of events that were allowed by this rate limiter since creation.longReturns the currently available number of permits.doubleReturns the average rate at which events have been accepted since creation.longReturns the maximum burst size.longReturns the sustained rate of permits.longReturns the total number of events that were rejected by this rate limiter since creation.longReturns the total number of events evaluated by this limiter.Returns the duration for which this rate limiter has been active.booleanAttempts to acquire a single permit.static TokenBucketRateLimiterGenerates a limiter that allows unlimited events.
-
Constructor Details
-
TokenBucketRateLimiter
public TokenBucketRateLimiter(long permitsPerSecond, long maxBurst) Creates a new rate limiter.- Parameters:
permitsPerSecond- sustained rate of permitsmaxBurst- maximum number of permits that can accumulate
-
-
Method Details
-
unlimited
Generates a limiter that allows unlimited events.- Returns:
- an unlimited rate limiter.
-
tryAcquire
public boolean tryAcquire()Attempts to acquire a single permit.- Returns:
trueif the event is allowed,falseotherwise
-
getPermitsPerSecond
public long getPermitsPerSecond()Returns the sustained rate of permits.- Returns:
- permits per second
-
getMaxBurst
public long getMaxBurst()Returns the maximum burst size.- Returns:
- maximum number of permits that can accumulate
-
getAvailableTokens
public long getAvailableTokens()Returns the currently available number of permits.- Returns:
- available permits
-
getAcceptedEvents
public long getAcceptedEvents()Returns the total number of events that were allowed by this rate limiter since creation.- Returns:
- number of accepted events
-
getRejectedEvents
public long getRejectedEvents()Returns the total number of events that were rejected by this rate limiter since creation.- Returns:
- number of rejected events
-
getTotalEvents
public long getTotalEvents()Returns the total number of events evaluated by this limiter. This is the sum of accepted and rejected events.- Returns:
- total number of events
-
getAcceptanceRatio
public double getAcceptanceRatio()Returns the ratio of accepted events to total events. A value of1.0indicates that no events have been rejected. If no events have been observed yet, this method returns1.0.- Returns:
- acceptance ratio in the range
[0.0, 1.0]
-
getUptime
Returns the duration for which this rate limiter has been active. The returned duration is based on monotonic time and is not affected by system clock changes.- Returns:
- uptime duration
-
getAverageAcceptedRatePerSecond
public double getAverageAcceptedRatePerSecond()Returns the average rate at which events have been accepted since creation. This value represents a coarse-grained average and is intended for diagnostics and reporting only.- Returns:
- average accepted events per second
-