Package org.apache.catalina.util
Class TimeBucketCounterBase
- java.lang.Object
-
- org.apache.catalina.util.TimeBucketCounterBase
-
- Direct Known Subclasses:
TimeBucketCounter
public abstract class TimeBucketCounterBase extends java.lang.Object
This class maintains a thread safe hash map that has timestamp-based buckets followed by a string for a key, and a counter for an integer value. Each time the increment() method is called it adds the key if it does not exist, increments its value and returns it.
-
-
Constructor Summary
Constructors Constructor Description TimeBucketCounterBase(int bucketDuration, java.util.concurrent.ScheduledExecutorService executorService)
Creates a new TimeBucketCounter with the specified lifetime.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
destroy()
Stops threads created by this object and cleans up resources.protected java.lang.String
genKey(java.lang.String identifier)
Generates the key of timeBucket counter maps with the specific identifier, and the timestamp is implicitly equivalent to "now".protected java.lang.String
genKey(java.lang.String identifier, long timestamp)
Generates the key of timeBucket counter maps with the specific identifier and timestamp.int
getBucketDuration()
protected abstract long
getBucketIndex(long timestamp)
Calculate the bucket index for the specific timestamp.int
getCurrentBucketPrefix()
Returns current bucket prefixabstract long
getMillisUntilNextBucket()
When we want to test a full bucket duration we need to sleep until the next bucket starts.abstract double
getRatio()
Returns the ratio between the configured duration param and the actual duration.int
increment(java.lang.String identifier)
Increments the counter for the passed identifier in the current time bucket and returns the new value.void
periodicEvict()
Periodic evict, perform removal of obsolete bucket items.
-
-
-
Constructor Detail
-
TimeBucketCounterBase
public TimeBucketCounterBase(int bucketDuration, java.util.concurrent.ScheduledExecutorService executorService)
Creates a new TimeBucketCounter with the specified lifetime.- Parameters:
bucketDuration
- duration in seconds, e.g. for 1 minute pass 60executorService
- the executor service that will be used to run the maintenance task- Throws:
java.lang.NullPointerException
- if executorService isnull
.
-
-
Method Detail
-
getBucketDuration
public int getBucketDuration()
- Returns:
- bucketDuration in seconds
-
getRatio
public abstract double getRatio()
Returns the ratio between the configured duration param and the actual duration.- Returns:
- the ratio between the configured duration param and the actual duration.
-
increment
public final int increment(java.lang.String identifier)
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
-
genKey
protected final java.lang.String genKey(java.lang.String identifier)
Generates the key of timeBucket counter maps with the specific identifier, and the timestamp is implicitly equivalent to "now".- Parameters:
identifier
- an identifier for which we want to maintain count- Returns:
- key of timeBucket counter maps
-
genKey
protected final java.lang.String genKey(java.lang.String identifier, long timestamp)
Generates the key of timeBucket counter maps with the specific identifier and timestamp.- Parameters:
identifier
- of target requesttimestamp
- when target request received- Returns:
- key of timeBucket counter maps
-
getBucketIndex
protected abstract long getBucketIndex(long timestamp)
Calculate the bucket index for the specific timestamp.- Parameters:
timestamp
- the specific timestamp in milliseconds- Returns:
- prefix the bucket key prefix for the specific timestamp
-
getCurrentBucketPrefix
public int getCurrentBucketPrefix()
Returns current bucket prefix- Returns:
- bucket index
-
getMillisUntilNextBucket
public abstract long getMillisUntilNextBucket()
When we want to test a full bucket duration we need to sleep until the next bucket starts.WARNING: This method is used for test purpose.
- Returns:
- the number of milliseconds until the next bucket
-
destroy
public void destroy()
Stops threads created by this object and cleans up resources.
-
periodicEvict
public void periodicEvict()
Periodic evict, perform removal of obsolete bucket items. Absence of this operation may result in OOM after a long run.
-
-