@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; }
private PermitRequest createPermitRequest(long permits) { PermitRequest request = new PermitRequest(); request.setPermits(permits); request.setRequestorIdentifier("requestor"); request.setResource("test"); return request; }
@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; }
@Builder private BatchedPermitsRequester(String resourceId, String requestorIdentifier, long targetMillisBetweenRequests, RequestSender requestSender, MetricContext metricContext) { Preconditions.checkArgument(!Strings.isNullOrEmpty(resourceId), "Must provide a resource id."); Preconditions.checkArgument(!Strings.isNullOrEmpty(requestorIdentifier), "Must provide a requestor identifier."); this.permitBatchContainer = new PermitBatchContainer(); this.lock = new ReentrantLock(); this.newPermitsAvailable = this.lock.newCondition(); /** Ensures there is only one in-flight request at a time. */ this.requestSemaphore = new Semaphore(1); /** Number of not-yet-satisfied permits. */ this.permitsOutstanding = new SynchronizedAverager(); this.targetMillisBetweenRequests = targetMillisBetweenRequests > 0 ? targetMillisBetweenRequests : DEFAULT_TARGET_MILLIS_BETWEEN_REQUESTS; this.requestSender = requestSender; this.retryStatus = new RetryStatus(); this.basePermitRequest = new PermitRequest(); this.basePermitRequest.setResource(resourceId); this.basePermitRequest.setRequestorIdentifier(requestorIdentifier); this.restRequestTimer = metricContext == null ? null : metricContext.timer(REST_REQUEST_TIMER); this.restRequestHistogram = metricContext == null ? null : metricContext.histogram(REST_REQUEST_PERMITS_HISTOGRAM); this.callbackCounter = new AtomicLong(); }
PermitRequest permitRequest = this.basePermitRequest.copy(); permitRequest.setPermits(permits); permitRequest.setMinPermits((long) this.permitsOutstanding.getAverageWeightOrZero()); if (BatchedPermitsRequester.this.restRequestHistogram != null) { BatchedPermitsRequester.this.restRequestHistogram.update(permits);
@Test public void test() { ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig(); guiceServletConfig.initialize(ConfigFactory.empty()); Injector injector = guiceServletConfig.getInjector(); LimiterServerResource limiterServer = injector.getInstance(LimiterServerResource.class); PermitRequest request = new PermitRequest(); request.setPermits(10); request.setResource("myResource"); PermitAllocation allocation = limiterServer.getSync(new ComplexResourceKey<>(request, new EmptyRecord())); Assert.assertTrue(allocation.getPermits() >= 10); }
PermitRequest res1request = new PermitRequest(); res1request.setPermits(20); res1request.setResource(res1key.getResourceLimitedPath()); PermitRequest res2request = new PermitRequest(); res2request.setPermits(20); res2request.setResource(res2key.getResourceLimitedPath()); PermitRequest res3request = new PermitRequest(); res3request.setPermits(100000); res3request.setResource("res3"); Assert.assertTrue(limiterServer.getSync(new ComplexResourceKey<>(res3request, new EmptyRecord())).getPermits() >= res3request.getPermits());
String resourceId = request.getResource(); Timer limiterTimer = resourceContext.timer(LIMITER_TIMER_NAME); permitsRequestedMeter.mark(request.getPermits()); } else { ThrottlingPolicy policy = (ThrottlingPolicy) this.broker.getSharedResource(new ThrottlingPolicyFactory(), new SharedLimiterKey(request.getResource()));
@Test public void testMetrics() throws Exception { ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig(); guiceServletConfig.initialize(ConfigFactory.empty()); Injector injector = guiceServletConfig.getInjector(); LimiterServerResource limiterServer = injector.getInstance(LimiterServerResource.class); PermitRequest request = new PermitRequest(); request.setPermits(10); request.setResource("myResource"); limiterServer.getSync(new ComplexResourceKey<>(request, new EmptyRecord())); limiterServer.getSync(new ComplexResourceKey<>(request, new EmptyRecord())); limiterServer.getSync(new ComplexResourceKey<>(request, new EmptyRecord())); MetricContext metricContext = limiterServer.metricContext; Timer timer = metricContext.timer(LimiterServerResource.REQUEST_TIMER_NAME); Assert.assertEquals(timer.getCount(), 3); }
String resourceId = request.getResource(); Timer limiterTimer = resourceContext.timer(LIMITER_TIMER_NAME); permitsRequestedMeter.mark(request.getPermits()); } else { ThrottlingPolicy policy = (ThrottlingPolicy) this.broker.getSharedResource(new ThrottlingPolicyFactory(), new SharedLimiterKey(request.getResource()));
PermitRequest res1request = new PermitRequest(); res1request.setPermits(20); res1request.setResource(res1key.getResourceLimitedPath()); PermitRequest invalidRequest = new PermitRequest(); invalidRequest.setPermits(20); invalidRequest.setResource("invalidkey");
@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); } }
@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; }
@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 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) { 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 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; }