@Test public void blockedNotAdded() throws InterruptedException { RateLimiter rateLimiter = new RateLimiter(1, 100); assertTrue("Filling up", rateLimiter.tryAcquire()); Thread.sleep(50); assertFalse("Expired too early", rateLimiter.tryAcquire()); Thread.sleep(50 + THRESHOLD_MILLI); assertTrue("Request not released", rateLimiter.tryAcquire()); }
public boolean tryAcquire() { long now = System.nanoTime() / 1_000_000; long toBeExpired = now - expireTime; synchronized (this) { long oldest = requests[position]; if (oldest < toBeExpired) { requests[position] = now; position = (position + 1) % requests.length; return true; } return false; } }
RateLimiter { public boolean tryAcquire() { long now = System.nanoTime() / 1_000_000; long toBeExpired = now - expireTime; synchronized (this) { long oldest = requests[position]; if (oldest < toBeExpired) { requests[position] = now; position = (position + 1) % requests.length; return true; } return false; } } }
RateLimiter { public boolean tryAcquire() { long now = System.nanoTime() / 1_000_000; long toBeExpired = now - expireTime; synchronized (this) { long oldest = requests[position]; if (oldest < toBeExpired) { requests[position] = now; position = (position + 1) % requests.length; return true; } return false; } } RateLimiter(int maxLimit, long expireTime); }
RateLimiter { public boolean tryAcquire() { long now = System.nanoTime() / 1_000_000; long toBeExpired = now - expireTime; synchronized (this) { long oldest = requests[position]; if (oldest < toBeExpired) { requests[position] = now; position = (position + 1) % requests.length; return true; } return false; } } RateLimiter(int maxLimit, long expireTime); boolean tryAcquire(); }
RateLimiter { public boolean tryAcquire() { long now = System.nanoTime() / 1_000_000; long toBeExpired = now - expireTime; synchronized (this) { long oldest = requests[position]; if (oldest < toBeExpired) { requests[position] = now; position = (position + 1) % requests.length; return true; } return false; } } RateLimiter(int maxLimit, long expireTime); boolean tryAcquire();  }