Package org.apache.ignite.internal.util
Class BasicRateLimiter
- java.lang.Object
-
- org.apache.ignite.internal.util.BasicRateLimiter
-
public class BasicRateLimiter extends Object
The simplified version of Google Guava smooth rate limiter.
The primary feature of a rate limiter is its "stable rate", the maximum rate that is should allow at normal conditions. This is enforced by "throttling" incoming requests as needed, i.e. compute, for an incoming request, the appropriate throttle time, and make the calling thread wait as much.
The simplest way to maintain a rate of QPS is to keep the timestamp of the last granted request, and ensure that (1/QPS) seconds have elapsed since then. For example, for a rate of QPS=5 (5 tokens per second), if we ensure that a request isn't granted earlier than 200ms after the last one, then we achieve the intended rate. If a request comes and the last request was granted only 100ms ago, then we wait for another 100ms. At this rate, serving 15 fresh permits (i.e. for an acquire(15) request) naturally takes 3 seconds.
It is important to realize that such a limiter has a very superficial memory of the past: it only remembers the last request. if the limiter was unused for a long period of time, then a request arrived and was immediately granted? This limiter would immediately forget about that past underutilization.
-
-
Constructor Summary
Constructors Constructor Description BasicRateLimiter(double permitsPerSecond)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidacquire(long permits)Acquires the given number of permits from thisRateLimiter, blocking until the request can be granted.doublegetRate()booleanisUnlimited()voidsetRate(double permitsPerSecond)Updates the stable rate.
-
-
-
Method Detail
-
setRate
public void setRate(double permitsPerSecond)
Updates the stable rate.- Parameters:
permitsPerSecond- The new stable rate of thisRateLimiter, set0for unlimited rate.- Throws:
IllegalArgumentException- IfpermitsPerSecondis negative or zero.
-
getRate
public double getRate()
- Returns:
- The stable rate as
permits per seconds(0means that the rate is unlimited).
-
isUnlimited
public boolean isUnlimited()
- Returns:
Trueif the rate is not limited.
-
acquire
public void acquire(long permits) throws IgniteInterruptedCheckedExceptionAcquires the given number of permits from thisRateLimiter, blocking until the request can be granted. Tells the amount of time slept, if any.- Parameters:
permits- The number of permits to acquire.- Throws:
IllegalArgumentException- If the requested number of permits is negative or zero.IgniteInterruptedCheckedException
-
-