package io.tetrapod.core.utils;

/**
 * A rate gauge that keeps a history of event times
 */
public class RateLimiter {

   protected final long[] samples;
   protected int          cur = -1;
   protected int          len;
   protected int          perMillis;

   public RateLimiter(int max, int perMillis) {
      this.samples = new long[max];
      this.perMillis = perMillis;
   }

   public synchronized void mark() {
      cur = (cur + 1) % samples.length;
      samples[cur] = System.currentTimeMillis();
      len = Math.min(len + 1, samples.length);
   }

   public synchronized Long getLastValue() {
      if (len > 0) {
         return samples[cur];
      } else {
         return null;
      }
   }

   public synchronized Long getOldestValue() {
      if (len > 0) {
         if (len < samples.length) {
            return samples[0];
         } else {
            return samples[(cur + 1) % samples.length];
         }
      } else {
         return null;
      }
   }

   public synchronized int getLength() {
      return len;
   }

   public synchronized boolean shouldLimit() {
      if (len >= samples.length) {
         return (getLastValue() - getOldestValue()) < perMillis;
      } else {
         return false;
      }
   }

}

@@ -252,16 +252,154 @@
 rMillis;
+%0A   %0A   // If we're currently limiting, how many have been ignored during this period of limiting.%0A   protected int          ignoredCount;
 %0A%0A   pub
@@ -510,16 +510,45 @@
 Millis;%0A
+      this.ignoredCount = 0;%0A
    %7D%0A%0A  
@@ -1299,23 +1299,20 @@
         
-return 
+if (
 (getLast
@@ -1354,55 +1354,316 @@
 llis
-;%0A      %7D else %7B%0A         return false;%0A      %7D
+) %7B%0A            ignoredCount++;%0A            return true;%0A         %7D else %7B%0A            ignoredCount = 0;%0A            return false;            %0A         %7D%0A      %7D else %7B%0A         ignoredCount = 0;%0A         return false;%0A      %7D%0A   %7D%0A   %0A   public synchronized int getIgnoreCount() %7B%0A      return ignoredCount;
 %0A   

Add count of how many messages have been ignored to rate limiter