public class RateLimiter extends Object
After n samples, we can computer Ri as the "immediate rate" like: Ri = (n-1) / (timestamp - timestamp(0)) which is essentially the 1/average interval. Given a target rate, Rt ("maxFreqHz"), we can compute it like: Rt = n / (S + (timestamp - timestamp(0)) S here is how long to sleep before we invoke the next operation. Solving for S: S = n / Rt - (timestamp - timestamp(0))Original author of Scala version: Eric Bowman (2012-05-16) Rewritten to Java by: Rijn Buve (2012-07-24).
Modifier and Type | Class and Description |
---|---|
static interface |
RateLimiter.WorkFunction
Interface for worker function, passed as an argument to limit() function below.
|
Constructor and Description |
---|
RateLimiter(int maxFreqHz) |
RateLimiter(int maxFreqHz,
int burstFreqHz)
Create a rate limiter that will limit calls to 'maxFreqHz' in bursts which occur approximately every
'burstFreqHz'.
|
Modifier and Type | Method and Description |
---|---|
void |
limit(RateLimiter.WorkFunction workFunction)
Limit the execution of a work function to the frequency set in this limiter.
|
public RateLimiter(int maxFreqHz, int burstFreqHz)
maxFreqHz
- Max. frequency.burstFreqHz
- Burts frequency.public RateLimiter(int maxFreqHz)
public void limit(RateLimiter.WorkFunction workFunction)
workFunction
- Work function (interface, see below).Copyright © 2018 TomTom International BV. All rights reserved.