List of usage examples for com.google.common.util.concurrent RateLimiter create
public static RateLimiter create(double permitsPerSecond)
From source file:de.tuberlin.orp.benchmark.BenchmarkTool.java
private void startWarmupPhase(int warmupSteps, long warmupMillis, int maxRate, long duration) { AtomicInteger stepsCounter = new AtomicInteger(1); long period = warmupMillis / (warmupSteps - 1); double rateStep = maxRate / (double) warmupSteps; rateLimiter = RateLimiter.create(rateStep); ScheduledExecutorService warmupService = Executors.newSingleThreadScheduledExecutor(); warmupService.scheduleAtFixedRate(() -> { int cnt = stepsCounter.getAndIncrement(); if (cnt < warmupSteps) { rateLimiter.setRate(rateStep * (cnt + 1)); if (cnt == warmupSteps - 1) { Executors.newSingleThreadScheduledExecutor().schedule((Runnable) () -> { stopped.set(true);//w w w.j av a 2 s . c om System.out.println("Done sending."); }, duration, TimeUnit.MILLISECONDS); warmupService.shutdown(); } } }, period, period, TimeUnit.MILLISECONDS); }
From source file:com.amazonaws.service.apigateway.importer.impl.sdk.ApiGatewaySdkApiImporter.java
protected List<Resource> buildResourceList(RestApi api) { List<Resource> resourceList = new ArrayList<>(); Resources resources = api.getResources(); resourceList.addAll(resources.getItem()); LOG.debug("Building list of resources. Stack trace: ", new Throwable()); final RateLimiter rl = RateLimiter.create(2); while (resources._isLinkAvailable("next")) { rl.acquire();// w ww .j av a 2s . c om resources = resources.getNext(); resourceList.addAll(resources.getItem()); } return resourceList; }
From source file:com.netflix.priam.aws.S3FileSystem.java
@Inject public S3FileSystem(Provider<AbstractBackupPath> pathProvider, ICompression compress, final IConfiguration config, ICredential cred) { this.pathProvider = pathProvider; this.compress = compress; this.config = config; int threads = config.getMaxBackupUploadThreads(); LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(threads); this.executor = new BlockingSubmitThreadPoolExecutor(threads, queue, UPLOAD_TIMEOUT); double throttleLimit = config.getUploadThrottle(); rateLimiter = RateLimiter.create(throttleLimit < 1 ? Double.MAX_VALUE : throttleLimit); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); String mbeanName = MBEAN_NAME; try {//ww w.ja va 2s . c o m mbs.registerMBean(this, new ObjectName(mbeanName)); } catch (Exception e) { throw new RuntimeException(e); } s3Client = new AmazonS3Client(cred.getAwsCredentialProvider()); s3Client.setEndpoint(getS3Endpoint()); }
From source file:com.boozallen.cognition.ingest.storm.spout.StormKafkaSpout.java
@Override public void open(Map conf, final TopologyContext context, final SpoutOutputCollector collector) { setupKafkaSpout();/*from w w w . j av a 2 s. c o m*/ kafkaSpout.open(conf, context, collector); if (isRateLimited()) { rateLimiter = RateLimiter.create(permitsPerSecond); } }
From source file:com.generalbytes.batm.server.extensions.extra.bitcoin.exchanges.XChangeExchange.java
public XChangeExchange(ExchangeSpecification specification, String preferredFiatCurrency) { exchange = ExchangeFactory.INSTANCE.createExchange(specification); name = exchange.getExchangeSpecification().getExchangeName(); log = LoggerFactory.getLogger("batm.master.exchange." + name); rateLimiter = RateLimiter.create(getAllowedCallsPerSecond()); this.preferredFiatCurrency = preferredFiatCurrency; }
From source file:io.engineblock.activityimpl.input.TargetRateInput.java
private void updateRateLimiter(ActivityDef activityDef) { double rate = activityDef.getParams().getOptionalDoubleUnitCount("targetrate").orElse(Double.NaN); if (!Double.isNaN(rate)) { if (rateLimiter == null) { rateLimiter = RateLimiter.create(rate); } else {/*from w w w. j av a2s. c om*/ rateLimiter.setRate(rate); } Gauge<Double> rateGauge = new Gauge<Double>() { @Override public Double getValue() { return rateLimiter.getRate(); } }; ActivityMetrics.gauge(activityDef, "targetrate", rateGauge); logger.info("targetrate was set to:" + rate); } }
From source file:org.apache.bookkeeper.client.UpdateLedgerOp.java
/** * Update the bookie id present in the ledger metadata. * * @param oldBookieId/*from w w w.ja v a2 s . c o m*/ * current bookie id * @param newBookieId * new bookie id * @param rate * number of ledgers updating per second (default 5 per sec) * @param limit * maximum number of ledgers to update (default: no limit). Stop * update if reaching limit * @param progressable * report progress of the ledger updates * @throws IOException * if there is an error when updating bookie id in ledger * metadata * @throws InterruptedException * interrupted exception when update ledger meta */ public void updateBookieIdInLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) throws BKException, IOException { final ThreadFactoryBuilder tfb = new ThreadFactoryBuilder().setNameFormat("UpdateLedgerThread") .setDaemon(true); final ExecutorService executor = Executors.newSingleThreadExecutor(tfb.build()); final AtomicInteger issuedLedgerCnt = new AtomicInteger(); final AtomicInteger updatedLedgerCnt = new AtomicInteger(); final Future<?> updateBookieCb = executor.submit(new Runnable() { @Override public void run() { updateLedgers(oldBookieId, newBookieId, rate, limit, progressable); } private void updateLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) { try { final AtomicBoolean stop = new AtomicBoolean(false); final Set<Long> outstandings = Collections .newSetFromMap(new ConcurrentHashMap<Long, Boolean>()); final RateLimiter throttler = RateLimiter.create(rate); final Iterator<Long> ledgerItr = admin.listLedgers().iterator(); final CountDownLatch syncObj = new CountDownLatch(1); // iterate through all the ledgers while (ledgerItr.hasNext() && !stop.get()) { // throttler to control updates per second throttler.acquire(); final Long lId = ledgerItr.next(); final ReadLedgerMetadataCb readCb = new ReadLedgerMetadataCb(bkc, lId, oldBookieId, newBookieId); outstandings.add(lId); FutureCallback<Void> updateLedgerCb = new UpdateLedgerCb(lId, stop, issuedLedgerCnt, updatedLedgerCnt, outstandings, syncObj, progressable); Futures.addCallback(readCb.getFutureListener(), updateLedgerCb); issuedLedgerCnt.incrementAndGet(); if (limit != Integer.MIN_VALUE && issuedLedgerCnt.get() >= limit || !ledgerItr.hasNext()) { stop.set(true); } bkc.getLedgerManager().readLedgerMetadata(lId, readCb); } // waiting till all the issued ledgers are finished syncObj.await(); } catch (IOException ioe) { LOG.error("Exception while updating ledger", ioe); throw new RuntimeException("Exception while updating ledger", ioe.getCause()); } catch (InterruptedException ie) { LOG.error("Exception while updating ledger metadata", ie); Thread.currentThread().interrupt(); throw new RuntimeException("Exception while updating ledger", ie.getCause()); } } }); try { // Wait to finish the issued ledgers. updateBookieCb.get(); } catch (ExecutionException ee) { throw new IOException("Exception while updating ledger", ee); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new IOException("Exception while updating ledger", ie); } finally { executor.shutdown(); } }
From source file:com.streamsets.pipeline.stage.processor.http.HttpProcessor.java
/** {@inheritDoc} */ @Override// ww w . ja va 2s. co m protected List<ConfigIssue> init() { List<ConfigIssue> issues = super.init(); errorRecordHandler = new DefaultErrorRecordHandler(getContext()); // NOSONAR double rateLimit = conf.rateLimit > 0 ? (1000.0 / conf.rateLimit) : Double.MAX_VALUE; rateLimiter = RateLimiter.create(rateLimit); httpClientCommon.init(issues, getContext()); conf.dataFormatConfig.init(getContext(), conf.dataFormat, Groups.HTTP.name(), HttpClientCommon.DATA_FORMAT_CONFIG_PREFIX, issues); bodyVars = getContext().createELVars(); bodyEval = getContext().createELEval(REQUEST_BODY_CONFIG_NAME); if (issues.isEmpty()) { parserFactory = conf.dataFormatConfig.getParserFactory(); } return issues; }
From source file:com.nestedbird.modules.ratelimiter.RateLimiterAspect.java
/** * Handle soft rate limiting, this pauses the current thread * * @param key the methods key//from www . j a va 2 s . com * @param joinPoint the join point * @param limit the limit annotation data */ private void handleSoftRateLimiting(final String key, final JoinPoint joinPoint, final RateLimit limit) { if (limit.value() > 0) { final RateLimiter limiter = limiterMap.computeIfAbsent(key, name -> RateLimiter.create(limit.value())); limiter.acquire(); } }
From source file:org.apache.aurora.scheduler.scheduling.SchedulingModule.java
@Override protected void configure() { install(new PrivateModule() { @Override/*ww w. jav a 2 s . c o m*/ protected void configure() { bind(TaskGroups.TaskGroupsSettings.class) .toInstance(new TaskGroups.TaskGroupsSettings(FIRST_SCHEDULE_DELAY.get(), new TruncatedBinaryBackoff(INITIAL_SCHEDULE_PENALTY.get(), MAX_SCHEDULE_PENALTY.get()), RateLimiter.create(MAX_SCHEDULE_ATTEMPTS_PER_SEC.get()), MAX_TASKS_PER_SCHEDULE_ATTEMPT.get())); bind(RescheduleCalculatorImpl.RescheduleCalculatorSettings.class) .toInstance(new RescheduleCalculatorImpl.RescheduleCalculatorSettings( new TruncatedBinaryBackoff(INITIAL_FLAPPING_DELAY.get(), MAX_FLAPPING_DELAY.get()), FLAPPING_THRESHOLD.get(), MAX_RESCHEDULING_DELAY.get())); bind(RescheduleCalculator.class).to(RescheduleCalculatorImpl.class).in(Singleton.class); expose(RescheduleCalculator.class); bind(TaskGroups.class).in(Singleton.class); expose(TaskGroups.class); } }); PubsubEventModule.bindSubscriber(binder(), TaskGroups.class); bind(new TypeLiteral<Integer>() { }).annotatedWith(TaskGroups.SchedulingMaxBatchSize.class).toInstance(SCHEDULING_MAX_BATCH_SIZE.get()); bind(TaskGroups.TaskGroupBatchWorker.class).in(Singleton.class); addSchedulerActiveServiceBinding(binder()).to(TaskGroups.TaskGroupBatchWorker.class); install(new PrivateModule() { @Override protected void configure() { bind(new TypeLiteral<BiCache<String, TaskGroupKey>>() { }).in(Singleton.class); bind(BiCache.BiCacheSettings.class) .toInstance(new BiCache.BiCacheSettings(RESERVATION_DURATION.get(), "reservation")); bind(TaskScheduler.class).to(TaskScheduler.TaskSchedulerImpl.class); bind(TaskScheduler.TaskSchedulerImpl.class).in(Singleton.class); expose(TaskScheduler.class); } }); PubsubEventModule.bindSubscriber(binder(), TaskScheduler.class); install(new PrivateModule() { @Override protected void configure() { bind(TaskThrottler.class).in(Singleton.class); expose(TaskThrottler.class); } }); PubsubEventModule.bindSubscriber(binder(), TaskThrottler.class); }