@Test public void test() throws Exception { CountBasedLimiter countLimiter1 = new CountBasedLimiter(3); CountBasedLimiter countLimiter2 = new CountBasedLimiter(1); MultiLimiter multiLimiter = new MultiLimiter(countLimiter1, countLimiter2); // Can only take 1 permit (limiter2 has only 1 permit available) Assert.assertNotNull(multiLimiter.acquirePermits(1)); Assert.assertNull(multiLimiter.acquirePermits(1)); // limiter1 has 1 leftover permit (one consumed in the failed second permit above) Assert.assertNotNull(countLimiter1.acquirePermits(1)); Assert.assertNull(countLimiter1.acquirePermits(1)); // limiter2 has not leftover permits Assert.assertNull(countLimiter2.acquirePermits(1)); }
@Override public PermitAllocation computePermitAllocation(PermitRequest request) { long permits = request.getPermits(); long allocated = 0; try { if (limiter.acquirePermits(permits) != null) { allocated = permits; } else { throw new RestLiServiceException(HttpStatus.S_403_FORBIDDEN, "Not enough permits."); } } catch (InterruptedException ie) { // return no permits } PermitAllocation allocation = new PermitAllocation(); allocation.setPermits(allocated); allocation.setExpiration(Long.MAX_VALUE); if (allocated <= 0) { allocation.setMinRetryDelayMillis(60000); } return allocation; }
@Override public PermitAllocation computePermitAllocation(PermitRequest request) { long permits = request.getPermits(); long allocated = 0; try { if (limiter.acquirePermits(permits) != null) { allocated = permits; } else { throw new RestLiServiceException(HttpStatus.S_403_FORBIDDEN, "Not enough permits."); } } catch (InterruptedException ie) { // return no permits } PermitAllocation allocation = new PermitAllocation(); allocation.setPermits(allocated); allocation.setExpiration(Long.MAX_VALUE); if (allocated <= 0) { allocation.setMinRetryDelayMillis(60000); } return allocation; }