@Test public void testEagerGrantingIfUnderused() throws Exception { int qps = 100; DynamicTokenBucket limiter = new DynamicTokenBucket(qps, 10, 100); Thread.sleep(100); Assert.assertTrue(limiter.getPermits(1, 0, 100) > 4); }
public long getPermits(long requestedPermits, long minPermits, long timeoutMillis) { PermitsAndDelay permitsAndDelay = getPermitsAndDelay(requestedPermits, minPermits, timeoutMillis); if (permitsAndDelay.delay > 0) { try { Thread.sleep(permitsAndDelay.delay); } catch (InterruptedException ie) { return 0; } } return permitsAndDelay.permits; }
DynamicTokenBucket { public long getPermits(long requestedPermits, long minPermits, long timeoutMillis) { PermitsAndDelay permitsAndDelay = getPermitsAndDelay(requestedPermits, minPermits, timeoutMillis); if (permitsAndDelay.delay > 0) { try { Thread.sleep(permitsAndDelay.delay); } catch (InterruptedException ie) { return 0; } } return permitsAndDelay.permits; } }
DynamicTokenBucket { public long getPermits(long requestedPermits, long minPermits, long timeoutMillis) { PermitsAndDelay permitsAndDelay = getPermitsAndDelay(requestedPermits, minPermits, timeoutMillis); if (permitsAndDelay.delay > 0) { try { Thread.sleep(permitsAndDelay.delay); } catch (InterruptedException ie) { return 0; } } return permitsAndDelay.permits; } DynamicTokenBucket(long qps, long fullRequestTimeoutMillis, long maxBucketSizeMillis); }
DynamicTokenBucket { public long getPermits(long requestedPermits, long minPermits, long timeoutMillis) { PermitsAndDelay permitsAndDelay = getPermitsAndDelay(requestedPermits, minPermits, timeoutMillis); if (permitsAndDelay.delay > 0) { try { Thread.sleep(permitsAndDelay.delay); } catch (InterruptedException ie) { return 0; } } return permitsAndDelay.permits; } DynamicTokenBucket(long qps, long fullRequestTimeoutMillis, long maxBucketSizeMillis); PermitsAndDelay getPermitsAndDelay(long requestedPermits, long minPermits, long timeoutMillis); long getPermits(long requestedPermits, long minPermits, long timeoutMillis); }
DynamicTokenBucket { public long getPermits(long requestedPermits, long minPermits, long timeoutMillis) { PermitsAndDelay permitsAndDelay = getPermitsAndDelay(requestedPermits, minPermits, timeoutMillis); if (permitsAndDelay.delay > 0) { try { Thread.sleep(permitsAndDelay.delay); } catch (InterruptedException ie) { return 0; } } return permitsAndDelay.permits; } DynamicTokenBucket(long qps, long fullRequestTimeoutMillis, long maxBucketSizeMillis); PermitsAndDelay getPermitsAndDelay(long requestedPermits, long minPermits, long timeoutMillis); long getPermits(long requestedPermits, long minPermits, long timeoutMillis);  }