public class Throttler extends Object
Limits an event rate using the event time marks.
The limitation of the event rate is based on tracking of timestamps, that are associated with each traceable event. The timestamp is just a long number, that continuously increased or at least keeps the same between two sequential events. In particular cases it can be "milliseconds since epoch", "microseconds since epoch", or something other - this must be considered by the users code.
The allowed event rate is specified as "the maximum number of events within the given interval", where the interval must be specified using the same measurement units, as ones used for time marks of events. Talking more formally, the allowed rate is a maximum number of sequential events where the difference between time marks of the first and the last event is less or equal to the specified interval.
The rate specification is passed via constructor parameters during construction of a new instance of the
throttler, or via reset(int, long)
method.
Tracing of events is implemented by the tryAdd(long)
method. This method returns a value that denotes
"allowing" or "disallowing" for the given event:
In the most trivial case, if the time mark is measured in milliseconds and obtained from the
System.currentTimeMillis()
call, the non-zero value returned by tryAdd(long)
method denotes
the number of milliseconds that have to be passed to the subsequent Thread.sleep(long)
in the user's
code.
Throttler throttler = new Throttler(NUMBER_OF_EVENTS, INTERVAL_IN_MILLIS);
// ...
while(!stopCondition) {
long wait = throttler.tryAdd(System.currentTimeMillis());
if (wait == 0) {
break; // Time mark added
}
Thread.sleep(wait); // Wait for the required time
}
Each throttler can be "active" and "inactive":
reset(int, long)
method.reset()
method has been
used there. Calls to the tryAdd(long)
method always return zero.Constructor and Description |
---|
Throttler()
Creates the inactive throttler.
|
Throttler(int maxEvents,
long interval)
Creates the active throttler.
|
Modifier and Type | Method and Description |
---|---|
long |
getInterval()
Returns interval duration.
|
int |
getMaxEvents()
Returns maximum number of events for the given interval.
|
boolean |
isActive()
Checks if the throttler active.
|
void |
reset()
Deactivates the throttler.
|
void |
reset(int maxEvents,
long interval)
Resets the throttler.
|
String |
toString()
String representation of the throttler.
|
long |
tryAdd(long timeMark)
Tries to put new time mark into internal storage of the throttler.
|
public Throttler()
The inactive throttler always "allows" events, i.e. its tryAdd(long)
method returns zero. The
throttler can be activated by the reset(int, long)
method.
public Throttler(int maxEvents, long interval)
The active throttler has a valid event rate specification, and its tryAdd(long)
method returns a
value depending on the actual event rate.
maxEvents
- Maximum number of events allowed within the given interval. The value must be greater than zero.interval
- The interval length. The value must be greater than zero.public boolean isActive()
public int getMaxEvents()
public long getInterval()
public void reset(int maxEvents, long interval)
Resets the throttler or turns it into active state. All stored time marks removed.
maxEvents
- Maximum number of events allowed within the given interval. The value must be greater than zero.interval
- The interval length. The value must be greater than zero.public void reset()
Turns the throttler to the "inactive" state, so all the tryAdd(long)
calls will return zero.
public long tryAdd(long timeMark)
timeMark
- The mark to add.Copyright © 2005–2024 Onix Solutions. All rights reserved.