@Test public void test() throws Exception { int qps = 10; DynamicTokenBucket limiter = new DynamicTokenBucket(qps, 10, 0); Assert.assertEquals(limiter.getPermits(10 * qps, 10 * qps, 1000), 0); long permits = qps / 5; Assert.assertEquals(limiter.getPermits(permits, permits, 300), permits); permits = qps; Assert.assertTrue(limiter.getPermits(permits, qps / 10, 200) >= qps / 10); }
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);  }