static Limiter createLimiter(Configuration configuration, SharedResourcesBroker<SimpleScopeType> broker) { try { Limiter limiter = new NoopLimiter(); long localQps = configuration.getLong(LOCALLY_ENFORCED_QPS, 0); if (localQps > 0) { log.info("Setting up local qps " + localQps); limiter = new MultiLimiter(limiter, new RateBasedLimiter(localQps)); } if (configuration.getBoolean(USE_THROTTLING_SERVER, false)) { log.info("Setting up remote throttling."); String resourceId = configuration.get(RESOURCE_ID); Limiter globalLimiter = broker.getSharedResource(new RestliLimiterFactory<SimpleScopeType>(), new SharedLimiterKey(resourceId)); limiter = new MultiLimiter(limiter, globalLimiter); } return limiter; } catch (NotConfiguredException nce) { throw new RuntimeException(nce); } }
/** * Throttles an {@link InputStream} if throttling is configured. * @param inputStream {@link InputStream} to throttle. * @param sourceURI used for selecting the throttling policy. * @param targetURI used for selecting the throttling policy. */ @Builder(builderMethodName = "throttleInputStream", builderClassName = "InputStreamThrottler") private ThrottledInputStream doThrottleInputStream(InputStream inputStream, URI sourceURI, URI targetURI) { Preconditions.checkNotNull(inputStream, "InputStream cannot be null."); Limiter limiter = new NoopLimiter(); if (sourceURI != null && targetURI != null) { StreamCopierSharedLimiterKey key = new StreamCopierSharedLimiterKey(sourceURI, targetURI); try { limiter = new MultiLimiter(limiter, this.broker.getSharedResource(new SharedLimiterFactory<S>(), key)); } catch (NotConfiguredException nce) { log.warn("Could not create a Limiter for key " + key, nce); } } else { log.info("Not throttling input stream because source or target URIs are not defined."); } Optional<MeteredInputStream> meteredStream = MeteredInputStream.findWrappedMeteredInputStream(inputStream); if (!meteredStream.isPresent()) { meteredStream = Optional.of(MeteredInputStream.builder().in(inputStream).build()); inputStream = meteredStream.get(); } return new ThrottledInputStream(inputStream, limiter, meteredStream.get()); }
throw new NotConfiguredException(); limiter = new NoopLimiter();
public void testConstructor() throws Exception { CountBasedLimiter countLimiter1 = new CountBasedLimiter(3); CountBasedLimiter countLimiter2 = new CountBasedLimiter(3); CountBasedLimiter countLimiter3 = new CountBasedLimiter(3); NoopLimiter noopLimiter = new NoopLimiter(); MultiLimiter multiLimiter1 = new MultiLimiter(countLimiter1, countLimiter2); Assert.assertEquals(multiLimiter1.getUnderlyingLimiters(), Lists.newArrayList(countLimiter1, countLimiter2)); // Noop limiters get filtered MultiLimiter multiLimiter2 = new MultiLimiter(countLimiter1, noopLimiter); Assert.assertEquals(multiLimiter2.getUnderlyingLimiters(), Lists.newArrayList(countLimiter1)); // multilimiters get expanded MultiLimiter multiLimiter3 = new MultiLimiter(multiLimiter1, countLimiter3); Assert.assertEquals(multiLimiter3.getUnderlyingLimiters(), Lists.newArrayList(countLimiter1, countLimiter2, countLimiter3)); // deduplication MultiLimiter multiLimiter4 = new MultiLimiter(countLimiter1, countLimiter1); Assert.assertEquals(multiLimiter4.getUnderlyingLimiters(), Lists.newArrayList(countLimiter1)); // deduplication on expanded multilimiters MultiLimiter multiLimiter5 = new MultiLimiter(multiLimiter1, countLimiter1); Assert.assertEquals(multiLimiter5.getUnderlyingLimiters(), Lists.newArrayList(countLimiter1, countLimiter2)); }
/** * Throttles an {@link InputStream} if throttling is configured. * @param inputStream {@link InputStream} to throttle. * @param sourceURI used for selecting the throttling policy. * @param targetURI used for selecting the throttling policy. */ @Builder(builderMethodName = "throttleInputStream", builderClassName = "InputStreamThrottler") private ThrottledInputStream doThrottleInputStream(InputStream inputStream, URI sourceURI, URI targetURI) { Preconditions.checkNotNull(inputStream, "InputStream cannot be null."); Limiter limiter = new NoopLimiter(); if (sourceURI != null && targetURI != null) { StreamCopierSharedLimiterKey key = new StreamCopierSharedLimiterKey(sourceURI, targetURI); try { limiter = new MultiLimiter(limiter, this.broker.getSharedResource(new SharedLimiterFactory<S>(), key)); } catch (NotConfiguredException nce) { log.warn("Could not create a Limiter for key " + key, nce); } } else { log.info("Not throttling input stream because source or target URIs are not defined."); } Optional<MeteredInputStream> meteredStream = MeteredInputStream.findWrappedMeteredInputStream(inputStream); if (!meteredStream.isPresent()) { meteredStream = Optional.of(MeteredInputStream.builder().in(inputStream).build()); inputStream = meteredStream.get(); } return new ThrottledInputStream(inputStream, limiter, meteredStream.get()); }
throw new NotConfiguredException(); limiter = new NoopLimiter();