com.google.common.util.concurrent.RateLimiter Java Examples
The following examples show how to use
com.google.common.util.concurrent.RateLimiter.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DelayExpPush.java From DDMQ with Apache License 2.0 | 6 votes |
private static void init(final String configPath) throws Exception { pushConfig = ConfigUtils.newConfig(configPath, BenchmarkConfig.class).getPushConfig(); pushConfig.setQpsLimit(pushConfig.getQpsLimit() == 0 ? 400000 : pushConfig.getQpsLimit()); pushConfig.setRunTimeMinute(pushConfig.getRunTimeMinute() == 0 ? Integer.MAX_VALUE : pushConfig.getRunTimeMinute()); CarreraConfig carreraConfig = new CarreraConfig(); carreraConfig.setCarreraProxyList(pushConfig.getPproxyAddrs()); carreraConfig.setCarreraProxyTimeout(pushConfig.getProxyTimeoutMs()); carreraConfig.setCarreraClientRetry(pushConfig.getClientRetry()); carreraConfig.setCarreraClientTimeout(pushConfig.getClientTimeoutMs()); carreraConfig.setCarreraPoolSize(pushConfig.getPoolSize()); producer = new CarreraProducer(carreraConfig); producer.start(); rateLimiter = RateLimiter.create(pushConfig.getQpsLimit()); executor = Executors.newFixedThreadPool(pushConfig.getThreadNum()); scheduler.schedule(() -> { stop = true; executor.shutdown(); }, pushConfig.getRunTimeMinute(), TimeUnit.MINUTES); }
Example #2
Source File: SimpleRateLimiter.java From qconfig with MIT License | 6 votes |
@QMapConfig(value = "config.properties", key = ADMIN_RATE_LIMIT) private void onLoad(String config) { // config like this : key1:速率,key2:速率(单位秒) if (currentConfig.equals(config)) { // 如果config.properties文件改动,但本key并无变化,不允许触发本速率限制变更 return; } final Map<String, Integer> limiter = parse(config); Map<String, RateLimiter> newRateLimiterMap = Maps.newConcurrentMap(); for (Map.Entry<String, Integer> entry : limiter.entrySet()) { newRateLimiterMap.put(entry.getKey(), RateLimiter.create(entry.getValue())); } rateLimiterMap = newRateLimiterMap; currentConfig = config; }
Example #3
Source File: TpsLimiter.java From DDMQ with Apache License 2.0 | 6 votes |
@Override public void updateNodeConfig(double warningRatio, int totalLimit) { if (this.totalLimit != totalLimit) { totalCount = RateLimiter.create(totalLimit * warningRatio); totalWarnCount = RateLimiter.create(totalLimit * (1 - warningRatio)); this.totalLimit = totalLimit; } if (this.warningRatio != warningRatio) { this.warningRatio = warningRatio; if (topicConfigInfo != null) { doUpdateConfig(topicConfigInfo, true); } } LOGGER.info("update rate limiter of node, totalCount:{}, warningRatio:{}", this.totalLimit, this.warningRatio); }
Example #4
Source File: ClientTest.java From TakinRPC with Apache License 2.0 | 6 votes |
public static void main(String[] args) { try { RateLimiter limit = RateLimiter.create(100d); PropertyConfigurator.configure("conf/log4j.properties"); while (true) { if (limit.tryAcquire()) { final HelloCommand command = new HelloCommand(); // System.out.println("result: " + command.execute()); // System.out.println(""); Future<String> future = command.queue(); System.out.println("result: " + future.get()); System.out.println(""); } } // Observable<String> observe = command.observe(); // observe.asObservable().subscribe((result) -> { // System.out.println(result); // }); } catch (Exception e) { e.printStackTrace(); } }
Example #5
Source File: RateLimiterLongRunningUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenLimitedResource_whenUseRateLimiter_thenShouldLimitPermits() { //given RateLimiter rateLimiter = RateLimiter.create(100); //when long startTime = ZonedDateTime.now().getSecond(); IntStream.range(0, 1000).forEach(i -> { rateLimiter.acquire(); doSomeLimitedOperation(); }); long elapsedTimeSeconds = ZonedDateTime.now().getSecond() - startTime; //then assertThat(elapsedTimeSeconds >= 10); }
Example #6
Source File: BalancerTool.java From distributedlog with Apache License 2.0 | 6 votes |
protected void parseCommandLine(CommandLine cmdline) throws ParseException { if (cmdline.hasOption("rwm")) { this.rebalanceWaterMark = Integer.parseInt(cmdline.getOptionValue("rwm")); } if (cmdline.hasOption("rtp")) { this.rebalanceTolerancePercentage = Double.parseDouble(cmdline.getOptionValue("rtp")); } if (cmdline.hasOption("rc")) { this.rebalanceConcurrency = Integer.parseInt(cmdline.getOptionValue("rc")); } if (cmdline.hasOption("r")) { this.rate = Double.parseDouble(cmdline.getOptionValue("r")); } checkArgument(rebalanceWaterMark >= 0, "Rebalance Water Mark should be a non-negative number"); checkArgument(rebalanceTolerancePercentage >= 0.0f, "Rebalance Tolerance Percentage should be a non-negative number"); checkArgument(rebalanceConcurrency > 0, "Rebalance Concurrency should be a positive number"); if (null == rate || rate <= 0.0f) { rateLimiter = Optional.absent(); } else { rateLimiter = Optional.of(RateLimiter.create(rate)); } }
Example #7
Source File: DelayLoopPush.java From DDMQ with Apache License 2.0 | 6 votes |
private static void init(final String configPath) throws Exception { pushConfig = ConfigUtils.newConfig(configPath, BenchmarkConfig.class).getPushConfig(); pushConfig.setQpsLimit(pushConfig.getQpsLimit() == 0 ? 400000 : pushConfig.getQpsLimit()); pushConfig.setRunTimeMinute(pushConfig.getRunTimeMinute() == 0 ? Integer.MAX_VALUE : pushConfig.getRunTimeMinute()); CarreraConfig carreraConfig = new CarreraConfig(); carreraConfig.setCarreraProxyList(pushConfig.getPproxyAddrs()); carreraConfig.setCarreraProxyTimeout(pushConfig.getProxyTimeoutMs()); carreraConfig.setCarreraClientRetry(pushConfig.getClientRetry()); carreraConfig.setCarreraClientTimeout(pushConfig.getClientTimeoutMs()); carreraConfig.setCarreraPoolSize(pushConfig.getPoolSize()); producer = new CarreraProducer(carreraConfig); producer.start(); rateLimiter = RateLimiter.create(pushConfig.getQpsLimit()); executor = Executors.newFixedThreadPool(pushConfig.getThreadNum()); scheduler.schedule(() -> { stop = true; executor.shutdown(); }, pushConfig.getRunTimeMinute(), TimeUnit.MINUTES); }
Example #8
Source File: DelayExpPush.java From DDMQ with Apache License 2.0 | 6 votes |
private static void init(final String configPath) throws Exception { pushConfig = ConfigUtils.newConfig(configPath, BenchmarkConfig.class).getPushConfig(); pushConfig.setQpsLimit(pushConfig.getQpsLimit() == 0 ? 400000 : pushConfig.getQpsLimit()); pushConfig.setRunTimeMinute(pushConfig.getRunTimeMinute() == 0 ? Integer.MAX_VALUE : pushConfig.getRunTimeMinute()); CarreraConfig carreraConfig = new CarreraConfig(); carreraConfig.setCarreraProxyList(pushConfig.getPproxyAddrs()); carreraConfig.setCarreraProxyTimeout(pushConfig.getProxyTimeoutMs()); carreraConfig.setCarreraClientRetry(pushConfig.getClientRetry()); carreraConfig.setCarreraClientTimeout(pushConfig.getClientTimeoutMs()); carreraConfig.setCarreraPoolSize(pushConfig.getPoolSize()); producer = new CarreraProducer(carreraConfig); producer.start(); rateLimiter = RateLimiter.create(pushConfig.getQpsLimit()); executor = Executors.newFixedThreadPool(pushConfig.getThreadNum()); scheduler.schedule(() -> { stop = true; executor.shutdown(); }, pushConfig.getRunTimeMinute(), TimeUnit.MINUTES); }
Example #9
Source File: RollupService.java From disthene with MIT License | 6 votes |
private RateLimiter getFlushRateLimiter(int currentBatch) { /* The idea is that we'd like to be able to process ALL the contents of accumulator in 1/2 time till next rollup time. Doing so, we hope to never limit as much as to saturate the accumulator and to heavily fall back */ // Get the smallest rollup - we can never get here if there are no rollups at all Rollup rollup = rollups.get(0); long timestamp = System.currentTimeMillis() / 1000; // Get the deadline - next rollup border long deadline = getRollupTimestamp(timestamp, rollup); // Just in case if (deadline - timestamp <= 0) return null; // 100 is an arbitrary small number here double rate = Math.max(100, 2 * currentBatch / (deadline - timestamp)); return RateLimiter.create(rate); }
Example #10
Source File: DataStoreUpdateThrottleTest.java From emodb with Apache License 2.0 | 6 votes |
@Test public void testInstanceRateLimited() throws Exception { final RateLimiter keyRateLimiter = mock(RateLimiter.class); final RateLimiter instanceRateLimiter = mock(RateLimiter.class); when(_rateLimiterFactory.create(10)).thenReturn(keyRateLimiter); when(_rateLimiterFactory.create(20)).thenReturn(instanceRateLimiter); updateAPIKeyRateLimit(_limitedId, new DataStoreUpdateThrottle(10, _now.plusSeconds(1))); updateAPIKeyRateLimit("*", new DataStoreUpdateThrottle(20, _now.plusSeconds(1))); createClient(API_KEY_LIMITED).update("table", "key", TimeUUIDs.newUUID(), Deltas.delete(), new AuditBuilder().build()); createClient(API_KEY_UNLIMITED).update("table", "key", TimeUUIDs.newUUID(), Deltas.delete(), new AuditBuilder().build()); verify(_rateLimiterFactory).create(10); verify(_rateLimiterFactory).create(20); verify(keyRateLimiter).acquire(); verify(instanceRateLimiter, times(2)).acquire(); }
Example #11
Source File: AsyncLimitUserAdvisor.java From BlogManagePlatform with Apache License 2.0 | 6 votes |
/** * AOP切点 * @author Frodez * @date 2019-05-10 */ @Override public Advice getAdvice() { /** * 请求限流 * @param JoinPoint AOP切点 * @author Frodez * @date 2018-12-21 */ return (MethodInterceptor) invocation -> { Pair<RateLimiter, Long> pair = limitCache.get(ReflectUtil.fullName(invocation.getMethod())); if (!pair.getKey().tryAcquire(pair.getValue(), DefTime.UNIT)) { return Result.busy().async(); } return invocation.proceed(); }; }
Example #12
Source File: LimitUserAdvisor.java From BlogManagePlatform with Apache License 2.0 | 6 votes |
/** * AOP切点 * @author Frodez * @date 2019-05-10 */ @Override public Advice getAdvice() { /** * 请求限流 * @param JoinPoint AOP切点 * @author Frodez * @date 2018-12-21 */ return (MethodInterceptor) invocation -> { Pair<RateLimiter, Long> pair = limitCache.get(ReflectUtil.fullName(invocation.getMethod())); if (!pair.getKey().tryAcquire(pair.getValue(), DefTime.UNIT)) { return Result.busy(); } return invocation.proceed(); }; }
Example #13
Source File: SumService.java From disthene with MIT License | 6 votes |
private void doFlush(Collection<Metric> metricsToFlush, RateLimiter rateLimiter) { logger.debug("Flushing metrics (" + metricsToFlush.size() + ")"); if (rateLimiter != null) { logger.debug("QPS is limited to " + (long) rateLimiter.getRate()); } for(Metric metric : metricsToFlush) { if (!blacklistService.isBlackListed(metric)) { if (!shuttingDown && rateLimiter != null) { rateLimiter.acquire(); } bus.post(new MetricStoreEvent(metric)).now(); } } }
Example #14
Source File: TpsLimiter.java From DDMQ with Apache License 2.0 | 6 votes |
@Override public void updateNodeConfig(double warningRatio, int totalLimit) { if (this.totalLimit != totalLimit) { totalCount = RateLimiter.create(totalLimit * warningRatio); totalWarnCount = RateLimiter.create(totalLimit * (1 - warningRatio)); this.totalLimit = totalLimit; } if (this.warningRatio != warningRatio) { this.warningRatio = warningRatio; if (topicConfigInfo != null) { doUpdateConfig(topicConfigInfo, true); } } LOGGER.info("update rate limiter of node, totalCount:{}, warningRatio:{}", this.totalLimit, this.warningRatio); }
Example #15
Source File: FetcherManagerTest.java From uReplicator with Apache License 2.0 | 5 votes |
@VisibleForTesting protected ConsumerFetcherThread createConsumerFetcherThread(String threadName, CustomizedConsumerConfig properties, RateLimiter rateLimiter, BlockingQueue<FetchedDataChunk> chunkQueue) { if (properties.containsKey(ConsumerConfig.CLIENT_ID_CONFIG)) { clientId.add(properties.getProperty(ConsumerConfig.CLIENT_ID_CONFIG)); } return mockConsumerFetcherThread; }
Example #16
Source File: DefaultBrokerConfigProvider.java From hermes with Apache License 2.0 | 5 votes |
public RateLimiter getPartitionProduceBytesRateLimiter(String topic, int partition) { Double limit = getLimit(m_topicBytesRateLimits.get(), topic); Pair<String, Integer> tp = new Pair<String, Integer>(topic, partition); RateLimiter rateLimiter = m_topicPartitionBytesRateLimiters.get(tp); if (rateLimiter == null) { synchronized (m_topicPartitionBytesRateLimiters) { rateLimiter = m_topicPartitionBytesRateLimiters.get(tp); if (rateLimiter == null) { rateLimiter = RateLimiter.create(limit); m_topicPartitionBytesRateLimiters.put(tp, rateLimiter); log.info( "Set single partition's bytes rate limit to {} for topic {}", limit, topic); } } } else { synchronized (rateLimiter) { if (rateLimiter.getRate() != limit) { rateLimiter.setRate(limit); log.info( "Single partition's bytes rate limit changed to {} for topic {}", limit, topic); } } } return rateLimiter; }
Example #17
Source File: SSTableScanner.java From stratio-cassandra with Apache License 2.0 | 5 votes |
public static ISSTableScanner getScanner(SSTableReader sstable, Collection<Range<Token>> tokenRanges, RateLimiter limiter) { // We want to avoid allocating a SSTableScanner if the range don't overlap the sstable (#5249) List<Pair<Long, Long>> positions = sstable.getPositionsForRanges(tokenRanges); if (positions.isEmpty()) return new EmptySSTableScanner(sstable.getFilename()); return new SSTableScanner(sstable, tokenRanges, limiter); }
Example #18
Source File: ClusterBalancer.java From distributedlog with Apache License 2.0 | 5 votes |
void moveRemainingStreamsFromSource(Host source, List<Host> hosts, Optional<RateLimiter> rateLimiter) { LinkedList<String> streamsToMove = new LinkedList<String>(source.streams); Collections.shuffle(streamsToMove); if (logger.isDebugEnabled()) { logger.debug("Try to move remaining streams from {} : {}", source, streamsToMove); } int hostIdx = hosts.size() - 1; while (!streamsToMove.isEmpty()) { if (rateLimiter.isPresent()) { rateLimiter.get().acquire(); } Host target = hosts.get(hostIdx); if (!target.address.equals(source.address)) { String stream = streamsToMove.remove(); // move the stream if (moveStream(stream, source, target)) { source.streams.remove(stream); target.streams.add(stream); } } --hostIdx; if (hostIdx < 0) { hostIdx = hosts.size() - 1; } } }
Example #19
Source File: FetcherManager.java From uReplicator with Apache License 2.0 | 5 votes |
@VisibleForTesting protected ConsumerFetcherThread createConsumerFetcherThread(String threadName, CustomizedConsumerConfig properties, RateLimiter rateLimiter, BlockingQueue<FetchedDataChunk> chunkQueue) { return new ConsumerFetcherThread(threadName, properties, rateLimiter, chunkQueue); }
Example #20
Source File: ProducerParticipant.java From qpid-broker-j with Apache License 2.0 | 5 votes |
public ProducerParticipant(final ClientJmsDelegate jmsDelegate, final CreateProducerCommand command) { _jmsDelegate = jmsDelegate; _command = command; _resultFactory = new ParticipantResultFactory(); _maximumDuration = _command.getMaximumDuration(); _numberOfMessages = _command.getNumberOfMessages(); _batchSize = _command.getBatchSize(); _acknowledgeMode = _jmsDelegate.getAcknowledgeMode(_command.getSessionName()); final double rate = _command.getRate(); _rateLimiter = (rate > 0 ? RateLimiter.create(rate) : null); }
Example #21
Source File: SimpleProducerConsumerStatTest.java From pulsar with Apache License 2.0 | 5 votes |
public void testBatchMessagesRateOut() throws PulsarClientException, InterruptedException, PulsarAdminException { log.info("-- Starting {} test --", methodName); String topicName = "persistent://my-property/cluster/my-ns/testBatchMessagesRateOut"; double produceRate = 17; int batchSize = 5; Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-subscriber-name") .subscribe(); Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).batchingMaxMessages(batchSize) .enableBatching(true).batchingMaxPublishDelay(2, TimeUnit.SECONDS).create(); AtomicBoolean runTest = new AtomicBoolean(true); Thread t1 = new Thread(() -> { RateLimiter r = RateLimiter.create(produceRate); while (runTest.get()) { r.acquire(); producer.sendAsync("Hello World".getBytes()); consumer.receiveAsync().thenAccept(consumer::acknowledgeAsync); } }); t1.start(); Thread.sleep(2000); // Two seconds sleep runTest.set(false); pulsar.getBrokerService().updateRates(); double actualRate = admin.topics().getStats(topicName).msgRateOut; assertTrue(actualRate > (produceRate / batchSize)); consumer.unsubscribe(); log.info("-- Exiting {} test --", methodName); }
Example #22
Source File: ClusterBalancer.java From distributedlog with Apache License 2.0 | 5 votes |
void moveRemainingStreamsFromSource(Host source, List<Host> hosts, Optional<RateLimiter> rateLimiter) { LinkedList<String> streamsToMove = new LinkedList<String>(source.streams); Collections.shuffle(streamsToMove); if (logger.isDebugEnabled()) { logger.debug("Try to move remaining streams from {} : {}", source, streamsToMove); } int hostIdx = hosts.size() - 1; while (!streamsToMove.isEmpty()) { if (rateLimiter.isPresent()) { rateLimiter.get().acquire(); } Host target = hosts.get(hostIdx); if (!target.address.equals(source.address)) { String stream = streamsToMove.remove(); // move the stream if (moveStream(stream, source, target)) { source.streams.remove(stream); target.streams.add(stream); } } --hostIdx; if (hostIdx < 0) { hostIdx = hosts.size() - 1; } } }
Example #23
Source File: JdbcBaseRunnable.java From datacollector with Apache License 2.0 | 5 votes |
public JdbcBaseRunnable( PushSource.Context context, int threadNumber, int batchSize, Map<String, String> offsets, MultithreadedTableProvider tableProvider, ConnectionManager connectionManager, TableJdbcConfigBean tableJdbcConfigBean, CommonSourceConfigBean commonSourceConfigBean, CacheLoader<TableRuntimeContext, TableReadContext> tableCacheLoader, RateLimiter queryRateLimiter, boolean isReconnect ) { this.jdbcUtil = UtilsProvider.getJdbcUtil(); this.context = context; this.threadNumber = threadNumber; this.lastQueryIntervalTime = -1; this.batchSize = batchSize; this.tableJdbcELEvalContext = new TableJdbcELEvalContext(context, context.createELVars()); this.offsets = offsets; this.tableProvider = tableProvider; this.tableJdbcConfigBean = tableJdbcConfigBean; this.commonSourceConfigBean = commonSourceConfigBean; this.connectionManager = connectionManager; this.tableReadContextCache = buildReadContextCache(tableCacheLoader); this.errorRecordHandler = new DefaultErrorRecordHandler(context, (ToErrorContext) context); this.numSQLErrors = 0; this.firstSqlException = null; this.isReconnect = isReconnect; // Metrics this.gaugeMap = context.createGauge(TABLE_METRICS + threadNumber).getValue(); this.queryRateLimiter = queryRateLimiter; }
Example #24
Source File: Backuper.java From cassandra-backup with Apache License 2.0 | 5 votes |
private Function<InputStream, InputStream> getUploadingInputStreamFunction(final AtomicBoolean shouldCancel) { return request.bandwidth == null ? identity() : inputStream -> { final RateLimiter rateLimiter = RateLimiter.create(request.bandwidth.asBytesPerSecond().value); logger.info("Upload bandwidth capped at {}.", request.bandwidth); return new RateLimitedInputStream(inputStream, rateLimiter, shouldCancel); }; }
Example #25
Source File: SimpleRateLimiter.java From qconfig with MIT License | 5 votes |
@Override public boolean takePermits(String key, Integer permits) { if (permits == null || permits <= 0) { return true; } final RateLimiter rateLimiter = rateLimiterMap.get(key); if (rateLimiter == null) { // 未对其做限速处理 return true; } return rateLimiter.tryAcquire(permits); }
Example #26
Source File: DistributedConsensusLoadTest.java From onos with Apache License 2.0 | 5 votes |
private void startTest() { stopped.set(false); RateLimiter limiter = RateLimiter.create(rate); Semaphore s = new Semaphore(100); while (!stopped.get()) { limiter.acquire(); s.acquireUninterruptibly(); counters.get(RandomUtils.nextInt(TOTAL_COUNTERS)).incrementAndGet().whenComplete((r, e) -> { s.release(); if (e == null) { increments.incrementAndGet(); } }); } }
Example #27
Source File: GoogleCloudStorageIntegrationTest.java From hadoop-connectors with Apache License 2.0 | 5 votes |
private static GoogleCloudStorage getGoogleCloudStorage( GoogleCloudStorageOptions.Builder optionsBuilder) throws IOException { Credential credential = GoogleCloudStorageTestHelper.getCredential(); return new ThrottledGoogleCloudStorage( // Allow 2 create or delete bucket operation every second. This will hit rate limits, // but GCS now has back-offs implemented for bucket operations. RateLimiter.create(2), new GoogleCloudStorageImpl(optionsBuilder.build(), credential), EnumSet.of(StorageOperation.DELETE_BUCKETS, StorageOperation.CREATE_BUCKET)); }
Example #28
Source File: GuavaRateLimiter.java From momo-cloud-permission with Apache License 2.0 | 5 votes |
public static void updateResourceRateLimiter(String resource, Double qps) { if (StringUtils.isBlank(resource)) { return; } if (qps == null) { qps = 50d; } //创建限流工具,每秒发出50个令牌指令 RateLimiter rateLimiter = RateLimiter.create(qps); resourceRateLimiter.put(resource, rateLimiter); }
Example #29
Source File: GuavaRateLimiter.java From momo-cloud-permission with Apache License 2.0 | 5 votes |
public static void createResourceRateLimiter(String resource, Double qps) { if (qps == null) { qps = 50d; } if (resourceRateLimiter.contains(resource)) { resourceRateLimiter.get(resource).setRate(qps); } else { //创建限流工具,每秒发出50个令牌指令 RateLimiter rateLimiter = RateLimiter.create(qps); resourceRateLimiter.put(resource, rateLimiter); } }
Example #30
Source File: Limiter3.java From blog with BSD 2-Clause "Simplified" License | 5 votes |
public static void main(String[] args) throws InterruptedException { RateLimiter limiter = RateLimiter.create(5); System.out.println(limiter.acquire(50)); System.out.println(limiter.acquire()); System.out.println(limiter.acquire()); System.out.println(limiter.acquire()); }