@Override public ThrottlingPolicy createPolicy(SharedLimiterKey key, SharedResourcesBroker<ThrottlingServerScopes> broker, Config config) { return new NoopPolicy(); } }
/** * @param qps the average qps desired. * @param fullRequestTimeoutMillis max time to fully satisfy a token request. This is generally a small timeout, on the * order of the network latency (e.g. ~100 ms). * @param maxBucketSizeMillis maximum number of unused tokens that can be stored during under-utilization time, in * milliseconds. The actual tokens stored will be 1000 * qps * maxBucketSizeMillis. */ DynamicTokenBucket(long qps, long fullRequestTimeoutMillis, long maxBucketSizeMillis) { this.tokenBucket = new TokenBucket(qps, maxBucketSizeMillis); this.baseTimeout = fullRequestTimeoutMillis; }
@Override public ThrottlingPolicy createPolicy(SharedLimiterKey key, SharedResourcesBroker<ThrottlingServerScopes> broker, Config config) { return new QPSPolicy(config); } }
@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; }
@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); }
@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 void sendRequest(PermitRequest request, Callback<Response<PermitAllocation>> callback) { PermitsGetRequestBuilder getBuilder = new PermitsRequestBuilders().get(); Request<PermitAllocation> fullRequest = getBuilder.id(new ComplexResourceKey<>(request, new EmptyRecord())).build(); getRestClient().sendRequest(fullRequest, decorateCallback(request, callback)); }
private synchronized void addPermitAllocation(PermitAllocation allocation) { this.batches.put(allocation.getExpiration(), new PermitBatch(allocation.getPermits(), allocation.getExpiration())); this.totalAvailablePermits += allocation.getPermits(); } }
@Override public void contextDestroyed(ServletContextEvent servletContextEvent) { close(); super.contextDestroyed(servletContextEvent); }
@Override public void close() { try { if (this._leaderFinder.isPresent()) { this._leaderFinder.get().stopAsync(); this._leaderFinder.get().awaitTerminated(2, TimeUnit.SECONDS); } } catch (TimeoutException te) { // Do nothing } }
private PermitAllocation sendRequestToServer(ThrottlingGuiceServletConfig guiceServletConfig, long permits) { return guiceServletConfig.getLimiterResource() .getSync(new ComplexResourceKey<>(createPermitRequest(10), new EmptyRecord())); }
@Override public Boolean call() { try { return this.tokenBucket.getTokens(this.tokens, this.timeoutMillis, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { throw new RuntimeException(ie); } } }
@Override protected void startUp() throws Exception { reset(); }
/** * Get the current number of stored tokens. Note this is a snapshot of the object, and there is no guarantee that those * tokens will be available at any point in the future. */ public long getStoredTokens() { synchronized (this) { updateTokensStored(System.currentTimeMillis()); } return (long) this.tokensStored; }
public TokenBucket(long qps, long maxBucketSizeInMillis) { this.nextTokenAvailableMillis = System.currentTimeMillis(); resetQPS(qps, maxBucketSizeInMillis); }
@Override protected void configureServlets() { serve("/*").with(GuiceRestliServlet.class); } });
@Test public void testSmallQps() throws Exception { testForQps(100); }
public void close() { this.server.stopAsync(); this.server.awaitTerminated(); this.guiceServletConfig.close(); } }
@Test public void testLargeQps() throws Exception { testForQps((long) 1e10); }