@Override public PermitAllocation computePermitAllocation(PermitRequest request) { PermitAllocation allocation = new PermitAllocation(); // For overflow safety, don't return max long allocation.setPermits(Math.max(Long.MAX_VALUE / 100, request.getPermits())); allocation.setExpiration(Long.MAX_VALUE); return allocation; }
@Override public void sendRequest(PermitRequest request, Callback<Response<PermitAllocation>> callback) { this.requestList.add(request); PermitAllocation permitAllocation = new PermitAllocation(); permitAllocation.setPermits(request.getPermits()); permitAllocation.setExpiration(Long.MAX_VALUE); Response<PermitAllocation> response = Mockito.mock(Response.class); Mockito.when(response.getEntity()).thenReturn(permitAllocation); callback.onSuccess(response); } }
Timer limiterTimer = resourceContext.timer(LIMITER_TIMER_NAME); permitsRequestedMeter.mark(request.getPermits());
@Builder(builderMethodName = "satisfyRequestBuilder", buildMethodName = "satisfy") public static void satisfyRequest(RequestAndCallback requestAndCallback, long expiration) { PermitAllocation allocation = new PermitAllocation(); allocation.setPermits(requestAndCallback.getRequest().getPermits()); allocation.setExpiration(expiration > 0 ? expiration : Long.MAX_VALUE); Response<PermitAllocation> response = Mockito.mock(Response.class); Mockito.when(response.getEntity()).thenReturn(allocation); requestAndCallback.getCallback().onSuccess(response); }
@Override public PermitAllocation computePermitAllocation(PermitRequest request) { long permitsRequested = request.getPermits(); Long minPermits = request.getMinPermits(GetMode.NULL); if (minPermits == null) { minPermits = permitsRequested; } long permitsGranted = this.tokenBucket.getPermits(permitsRequested, minPermits, LimiterServerResource.TIMEOUT_MILLIS); PermitAllocation allocation = new PermitAllocation(); allocation.setPermits(permitsGranted); allocation.setExpiration(Long.MAX_VALUE); if (permitsGranted <= 0) { allocation.setMinRetryDelayMillis(LimiterServerResource.TIMEOUT_MILLIS); } 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; }
Assert.assertTrue(limiterServer.getSync(new ComplexResourceKey<>(res3request, new EmptyRecord())).getPermits() >= res3request.getPermits());
@Override public PermitAllocation computePermitAllocation(PermitRequest request) { PermitAllocation allocation = new PermitAllocation(); // For overflow safety, don't return max long allocation.setPermits(Math.max(Long.MAX_VALUE / 100, request.getPermits())); allocation.setExpiration(Long.MAX_VALUE); return allocation; }
@Override public PermitAllocation computePermitAllocation(PermitRequest request) { long permitsRequested = request.getPermits(); Long minPermits = request.getMinPermits(GetMode.NULL); if (minPermits == null) { minPermits = permitsRequested; } long permitsGranted = this.tokenBucket.getPermits(permitsRequested, minPermits, LimiterServerResource.TIMEOUT_MILLIS); PermitAllocation allocation = new PermitAllocation(); allocation.setPermits(permitsGranted); allocation.setExpiration(Long.MAX_VALUE); if (permitsGranted <= 0) { allocation.setMinRetryDelayMillis(LimiterServerResource.TIMEOUT_MILLIS); } return allocation; }
Timer limiterTimer = resourceContext.timer(LIMITER_TIMER_NAME); permitsRequestedMeter.mark(request.getPermits());
@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; }