Class WindowRateControl

  • All Implemented Interfaces:
    RateControl

    public class WindowRateControl
    extends java.lang.Object
    implements RateControl

    An implementation of RateControl that limits the number of events within a time period.

    Events are kept in a queue and for each event the queue is first drained of the old events outside the time window, and then the new event is added to the queue. The size of the queue is maintained separately in an AtomicInteger and if it exceeds the max number of events then onEvent(Object) returns false.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Queue<java.lang.Long> events  
      private int maxEvents  
      private java.util.concurrent.atomic.AtomicInteger size  
      private long window  
    • Constructor Summary

      Constructors 
      Constructor Description
      WindowRateControl​(int maxEvents, java.time.Duration window)  
    • Field Detail

      • events

        private final java.util.Queue<java.lang.Long> events
      • size

        private final java.util.concurrent.atomic.AtomicInteger size
      • maxEvents

        private final int maxEvents
      • window

        private final long window
    • Constructor Detail

      • WindowRateControl

        public WindowRateControl​(int maxEvents,
                                 java.time.Duration window)
    • Method Detail

      • fromEventsPerSecond

        public static WindowRateControl fromEventsPerSecond​(int maxEvents)
      • getEventsPerSecond

        public int getEventsPerSecond()
      • onEvent

        public boolean onEvent​(java.lang.Object event)
        Description copied from interface: RateControl

        Applications should call this method when they want to signal an event that is subject to rate control.

        Implementations should return true if the event does not exceed the desired rate, or false to signal that the event exceeded the desired rate.

        Specified by:
        onEvent in interface RateControl
        Parameters:
        event - the event subject to rate control.
        Returns:
        true IFF the rate is within limits