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 SummaryConstructorsConstructorDescriptionTimeBucketCounter(int bucketDuration, ScheduledExecutorService executorService) Creates a new TimeBucketCounter with the specified lifetime.
- 
Method SummaryModifier and TypeMethodDescriptionvoiddestroy()Sets isRunning to false to terminate the maintenance thread.intThe 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 intCalculates 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.longWhen we want to test a full bucket duration we need to sleep until the next bucket starts.intdoublegetRatio()Returns the ratio between the configured duration param and the actual duration which will be set to the next power of 2.final intIncrements the counter for the passed identifier in the current time bucket and returns the new value.
- 
Constructor Details- 
TimeBucketCounterCreates a new TimeBucketCounter with the specified lifetime.- Parameters:
- bucketDuration- duration in seconds, e.g. for 1 minute pass 60
- executorService- the executor service which will be used to run the maintenance
 
 
- 
- 
Method Details- 
incrementIncrements 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
 
- 
getCurrentBucketPrefixpublic 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.
 
- 
getNumBitspublic int getNumBits()
- 
getActualDurationpublic 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
 
- 
getRatiopublic 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
 
- 
getMillisUntilNextBucketpublic 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
 
- 
destroypublic void destroy()Sets isRunning to false to terminate the maintenance thread.
 
-