Class TimeBucketCounter


  • public class TimeBucketCounter
    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 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

      Constructors 
      Constructor Description
      TimeBucketCounter​(int bucketDuration)
      Creates a new TimeBucketCounter with the specified lifetime.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void destroy()
      sets isRunning to false to terminate the maintenance thread
      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
      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
      long getMillisUntilNextBucket()
      when we want to test a full bucket duration we need to sleep until the next bucket starts
      int getNumBits()  
      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
      int increment​(java.lang.String identifier)
      increments the counter for the passed identifier in the current time bucket and returns the new value
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TimeBucketCounter

        public TimeBucketCounter​(int bucketDuration)
        Creates a new TimeBucketCounter with the specified lifetime.
        Parameters:
        bucketDuration - duration in seconds, e.g. for 1 minute pass 60
    • Method Detail

      • 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
      • 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