Example usage for com.google.common.util.concurrent RateLimiter create

List of usage examples for com.google.common.util.concurrent RateLimiter create

Introduction

In this page you can find the example usage for com.google.common.util.concurrent RateLimiter create.

Prototype



public static RateLimiter create(double permitsPerSecond) 

Source Link

Document

Creates a RateLimiter with the specified stable throughput, given as "permits per second" (commonly referred to as QPS, queries per second).

Usage

From source file:com.google.pubsub.clients.common.LoadTestRunner.java

private static void runTest(StartRequest request) {
    log.info("Request received, starting up server.");
    ListeningExecutorService executor = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(request.getMaxOutstandingRequests() + 10));

    final RateLimiter rateLimiter = RateLimiter.create(request.getRequestRate());
    final Semaphore outstandingTestLimiter = new Semaphore(request.getMaxOutstandingRequests(), false);

    final long toSleep = request.getStartTime().getSeconds() * 1000 - System.currentTimeMillis();
    if (toSleep > 0) {
        try {//from w  w  w  .  j av  a 2s.  c o  m
            Thread.sleep(toSleep);
        } catch (InterruptedException e) {
            log.error("Interrupted sleeping, starting test now.");
        }
    }

    stopwatch.start();
    while (shouldContinue(request)) {
        outstandingTestLimiter.acquireUninterruptibly();
        rateLimiter.acquire();
        executor.submit(task).addListener(outstandingTestLimiter::release, executor);
    }
    stopwatch.stop();
    executor.shutdownNow();
    finished.set(true);
    log.info("Load test complete.");
}

From source file:com.amazonaws.dynamodb.bootstrap.DynamoDBConsumer.java

/**
 * Class to consume logs and write them to a DynamoDB table.
 *//*  w  ww  .j av  a 2s .  co m*/
public DynamoDBConsumer(AmazonDynamoDBClient client, String tableName, double rateLimit, ExecutorService exec) {
    this.client = client;
    this.tableName = tableName;
    this.rateLimiter = RateLimiter.create(rateLimit);
    super.threadPool = exec;
    super.exec = new ExecutorCompletionService<Void>(threadPool);
}

From source file:at.becast.youploader.youtube.upload.UploadStream.java

public UploadStream(File file, UploadEvent event) throws FileNotFoundException {
    super(new FileInputStream(file), BUFFER_SIZE);
    this.rateLimiter = RateLimiter.create(Double.MAX_VALUE);
    this.event = event;
    this.limit = 0;
    this.size = file.length();
    this.position = 0;
    if (event != null) {
        event.onInit();// ww w.j a v a 2s.c  om
    }
}

From source file:org.cloudifysource.esc.driver.provisioning.RequestRateLimiter.java

/**
 * /*  w w  w.  j ava 2  s . com*/
 * @param numRequests - the number of allowed requests in the given time frame.
 * @param timeFrame - the timeframe limit for number of requests
 * @param unit - time unit.
 */
public RequestRateLimiter(final int numRequests, final long timeFrame, final TimeUnit unit) {
    this.numRequests = numRequests;
    this.throttler = RateLimiter.create(1.0 / unit.toSeconds(timeFrame));
}

From source file:com.google.cloud.trace.core.RateLimitingTraceOptionsFactory.java

/**
 * Creates a trace options factory that generates trace options based on a rate limiter which
 * limits the number of enabled traces according to the specified {@code samplingRate} and sets
 * the stack trace according to the specified {@code stackTraceEnabled} parameter.
 *
 * @param samplingRate a double that is the rate (unit 1/s) of generated trace options whose trace
 *     option is true./*from w w  w . j  ava2 s  .  co m*/
 * @param stackTraceEnabled the value of the new trace options' stack trace option
 */
public RateLimitingTraceOptionsFactory(double samplingRate, boolean stackTraceEnabled) {
    this(RateLimiter.create(samplingRate), stackTraceEnabled);
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.monitoring.RateLimiterService.java

public static void updateRates(String type, long rate) {
    LOGGER.debug("updateRates : type={}, rate={}", type, rate);
    Iterator<RateLimiterDescriptor> iterator = rateLimiterMap.asMap().keySet().iterator();
    while (iterator.hasNext()) {
        RateLimiterDescriptor oldRateDesc = iterator.next();
        if (type.equals(oldRateDesc.getType())) {
            LOGGER.trace("updateRates : invalidated rate={}", oldRateDesc);
            rateLimiterMap.invalidate(oldRateDesc);
            RateLimiterDescriptor newRateDesc = new RateLimiterDescriptor(oldRateDesc.getType(),
                    oldRateDesc.getAppId(), rate);
            LOGGER.trace("updateRates : updating rate desc={}, rate={}", newRateDesc, rate);
            rateLimiterMap.put(newRateDesc, RateLimiter.create(rate));
            rateLimiterMap.refresh(newRateDesc);
        }// www. j a  v a 2 s  .  com
    }
}

From source file:cherry.foundation.mail.SendMailBatch.java

private void sendMail() {
    try {//from   w w  w  .  j a  v  a 2  s . c  om

        RateLimiter rateLimiter = null;
        if (sendPerSecond != null && sendPerSecond.doubleValue() > 0.0) {
            rateLimiter = RateLimiter.create(sendPerSecond.doubleValue());
        }

        LocalDateTime now = bizDateTime.now();
        for (long messageId : mailSendHandler.listMessage(now)) {

            if (rateLimiter != null) {
                rateLimiter.acquire();
            }

            mailSendHandler.sendMessage(messageId);
        }
    } catch (MailException | DataAccessException ex) {
        if (log.isDebugEnabled()) {
            log.debug(ex, "failed to send mail");
        }
    }
}

From source file:com.ibm.og.scheduling.RequestRateScheduler.java

/**
 * Constructs an instance using the provided rate {@code count / unit }
 * /* ww w .  j av a 2s .  c o  m*/
 * @param rate the numerator of the rate to configure
 * @param unit the denominator of the rate to configure
 * @param rampup the duration to ramp up to the stable request rate
 * @param rampupUnit the rampup duration unit
 */
public RequestRateScheduler(final double rate, final TimeUnit unit, final double rampup,
        final TimeUnit rampupUnit) {
    checkArgument(rate >= 0.0, "rate must be >= 0.0 [%s]", rate);
    this.rate = rate;
    this.unit = checkNotNull(unit);
    checkArgument(rampup >= 0.0, "rampup must be >= 0.0 [%s]", rampup);
    this.rampup = rampup;
    this.rampupUnit = checkNotNull(rampupUnit);
    this.permits = new AtomicReference<RateLimiter>();

    // convert arbitrary rate unit to rate/second
    final double requestsPerSecond = requestsPerSecond(rate, unit);

    _logger.debug("Calculated requests per second [{}]", requestsPerSecond);

    if (DoubleMath.fuzzyEquals(rampup, 0.0, Math.pow(0.1, 6))) {
        final RateLimiter steady = RateLimiter.create(requestsPerSecond);
        this.permits.set(steady);
    } else {
        // the warmup Ratelimiter will not work if the permit request rate is slow enough to not being able to reach the
        // threshold from left. The permits are accumulated faster than the request rate here.
        // So the steady OPS will not be reached at all during warm up period.
        // Approximate the warm-up period with steady ratelimiter and set the ops for the steady rate limiter
        // based on the steady state ops, warm up duration.

        // calculate the ops based on the ramp duration and steady state ops
        final double slope = requestsPerSecond / (rampupUnit.toSeconds((long) rampup));
        final int rampStepWidth = calculateStepWidth(rate, rampup, rampupUnit);

        this.permits.set(RateLimiter.create(slope * rampStepWidth * 1));

        final Thread rampupThread = new Thread(new Runnable() {
            @Override
            public void run() {
                _logger.debug("Awaiting start latch");
                Uninterruptibles.awaitUninterruptibly(RequestRateScheduler.this.started);

                _logger.info("Starting ramp");

                double requestsPerSecondNow;
                RateLimiter rampRateLimiter;
                int rampStepNum = 1;
                int rampSteps = DoubleMath.roundToInt(((rampupUnit.toSeconds((long) rampup)) / rampStepWidth),
                        RoundingMode.DOWN);
                _logger.info("ramp profile rampStepWidth {}  NumRampSteps {} ", rampStepWidth, rampSteps);
                while (rampStepNum <= rampSteps) {
                    Uninterruptibles.sleepUninterruptibly(rampStepWidth * 1000L, TimeUnit.MILLISECONDS);
                    rampStepNum++;
                    requestsPerSecondNow = slope * rampStepWidth * rampStepNum;
                    _logger.debug("slope {} rampStep  {}  targetRequestPerSecond {} ", slope, rampStepNum,
                            requestsPerSecondNow);
                    rampRateLimiter = RateLimiter.create(requestsPerSecondNow);
                    RequestRateScheduler.this.permits.set(rampRateLimiter);
                }
                final RateLimiter steady = RateLimiter.create(requestsPerSecond);
                RequestRateScheduler.this.permits.set(steady);

                _logger.info("Finished ramp");
            }

        }, "rate-scheduler-ramp");
        rampupThread.setDaemon(true);
        rampupThread.start();
    }
    this.started = new CountDownLatch(1);
}

From source file:com.google.cloud.hadoop.gcsio.ThrottledGoogleCloudStorage.java

/**
 * @param operationsPerSecond Operations per second to allow.
 * @param wrappedGcs The GoogleCloudStorage that we should delegate operations to.
 * @param throttledOperations The operations that should be throttled.
 *///w  ww. j a  va 2 s. co  m
public ThrottledGoogleCloudStorage(double operationsPerSecond, GoogleCloudStorage wrappedGcs,
        EnumSet<StorageOperation> throttledOperations) {
    this(RateLimiter.create(operationsPerSecond), wrappedGcs, throttledOperations);
}

From source file:gobblin.util.limiter.RateBasedLimiter.java

public RateBasedLimiter(double rateLimit, TimeUnit timeUnit) {
    this.rateLimitPerSecond = convertRate(rateLimit, timeUnit, TimeUnit.SECONDS);
    this.rateLimiter = RateLimiter.create(this.rateLimitPerSecond);
}