Package org.apache.catalina.util
Class TimeBucketCounter
java.lang.Object
org.apache.catalina.util.TimeBucketCounter
This class maintains a thread safe hash map that has timestamp-based buckets followed by a string for a key, and a
counter for a value. each time the increment() method is called it adds the key if it does not exist, increments its
value and returns it. a maintenance thread cleans up keys that are prefixed by previous timestamp buckets.
-
Constructor Summary
ConstructorDescriptionTimeBucketCounter
(int bucketDuration, ScheduledExecutorService executorService) Creates a new TimeBucketCounter with the specified lifetime. -
Method Summary
Modifier and TypeMethodDescriptionvoid
destroy()
Sets isRunning to false to terminate the maintenance thread.int
The actual duration may differ from the configured duration because it is set to the next power of 2 value in order to perform very fast bit shift arithmetic.final int
Calculates the current time bucket prefix by shifting bits for fast division, e.g. shift 16 bits is the same as dividing by 65,536 which is about 1:05m.long
When we want to test a full bucket duration we need to sleep until the next bucket starts.int
double
getRatio()
Returns the ratio between the configured duration param and the actual duration which will be set to the next power of 2.final int
Increments the counter for the passed identifier in the current time bucket and returns the new value.
-
Constructor Details
-
TimeBucketCounter
Creates a new TimeBucketCounter with the specified lifetime.- Parameters:
bucketDuration
- duration in seconds, e.g. for 1 minute pass 60executorService
- the executor service which will be used to run the maintenance
-
-
Method Details
-
increment
Increments the counter for the passed identifier in the current time bucket and returns the new value.- Parameters:
identifier
- an identifier for which we want to maintain count, e.g. IP Address- Returns:
- the count within the current time bucket
-
getCurrentBucketPrefix
public final int getCurrentBucketPrefix()Calculates the current time bucket prefix by shifting bits for fast division, e.g. shift 16 bits is the same as dividing by 65,536 which is about 1:05m.- Returns:
- The current bucket prefix.
-
getNumBits
public int getNumBits() -
getActualDuration
public int getActualDuration()The actual duration may differ from the configured duration because it is set to the next power of 2 value in order to perform very fast bit shift arithmetic.- Returns:
- the actual bucket duration in milliseconds
-
getRatio
public double getRatio()Returns the ratio between the configured duration param and the actual duration which will be set to the next power of 2. We then multiply the configured requests param by the same ratio in order to compensate for the added time, if any.- Returns:
- the ratio, e.g. 1.092 if the actual duration is 65_536 for the configured duration of 60_000
-
getMillisUntilNextBucket
public long getMillisUntilNextBucket()When we want to test a full bucket duration we need to sleep until the next bucket starts.- Returns:
- the number of milliseconds until the next bucket
-
destroy
public void destroy()Sets isRunning to false to terminate the maintenance thread.
-