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.ibm.og.scheduling.ConcurrentRequestScheduler.java

/**
 * Constructs an instance with the provided concurrency
 * //w ww .j a v  a 2 s.  com
 * @param concurrentRequests the number of concurrent requests allowed
 * @throws IllegalArgumentException if concurrentRequests is negative or zero
 */
public ConcurrentRequestScheduler(final int concurrentRequests, final double rampup,
        final TimeUnit rampupUnit) {
    checkArgument(concurrentRequests > 0, "concurrentRequests must be > 0");
    checkArgument(rampup >= 0.0, "rampup must be >= 0.0 [%s]", rampup);
    checkNotNull(rampupUnit);
    this.concurrentRequests = concurrentRequests;
    this.rampup = rampup;
    this.rampupUnit = rampupUnit;
    this.started = new CountDownLatch(1);

    if (DoubleMath.fuzzyEquals(rampup, 0.0, Math.pow(0.1, 6))) {
        this.permits = new Semaphore(concurrentRequests);
    } else {
        this.permits = new Semaphore(0);
        final Thread rampupThread = new Thread(new Runnable() {
            @Override
            public void run() {
                final double rampSeconds = (rampup * rampupUnit.toNanos(1)) / TimeUnit.SECONDS.toNanos(1);
                _logger.debug("Ramp seconds [{}]", rampSeconds);

                final RateLimiter ramp = RateLimiter.create(concurrentRequests / rampSeconds);
                _logger.debug("Ramp rate [{}]", ramp.getRate());

                _logger.debug("Awaiting start latch");
                Uninterruptibles.awaitUninterruptibly(ConcurrentRequestScheduler.this.started);

                _logger.info("Starting ramp");
                for (int i = 0; i < concurrentRequests; i++) {
                    _logger.debug("Acquiring RateLimiter permit");
                    ramp.acquire();
                    _logger.debug("Releasing semaphore permit");
                    ConcurrentRequestScheduler.this.permits.release();
                }
                _logger.info("Finished ramp");
            }
        }, "concurrent-scheduler-ramp");
        rampupThread.setDaemon(true);
        rampupThread.start();
        _logger.debug("Starting permits [{}]", this.permits.availablePermits());
    }
}

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

protected Task(StartRequest request, String type, MetricsHandler.MetricName metricName) {
    this.metricsHandler = new MetricsHandler(request.getProject(), type, metricName);
    this.burnInTimeMillis = Timestamps
            .toMillis(Timestamps.add(request.getStartTime(), request.getBurnInDuration()));
    rateLimiter = RateLimiter.create(request.getRequestRate());
    outstandingRequestLimiter = new Semaphore(request.getMaxOutstandingRequests(), false);
}

From source file:org.apache.qpid.disttest.client.ProducerParticipant.java

public ProducerParticipant(final ClientJmsDelegate jmsDelegate, final CreateProducerCommand command) {
    _jmsDelegate = jmsDelegate;/*from   w w w. ja v  a 2  s.co  m*/
    _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);
}

From source file:org.apache.cassandra.stress.StressAction.java

public void run() {
    // creating keyspace and column families
    settings.maybeCreateKeyspaces();/*from  www. j  av  a2 s .c  o m*/

    output.println("Sleeping 2s...");
    Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);

    if (!settings.command.noWarmup)
        warmup(settings.command.getFactory(settings));
    if (settings.command.truncate == SettingsCommand.TruncateWhen.ONCE)
        settings.command.truncateTables(settings);

    // TODO : move this to a new queue wrapper that gates progress based on a poisson (or configurable) distribution
    RateLimiter rateLimiter = null;
    if (settings.rate.opRateTargetPerSecond > 0)
        rateLimiter = RateLimiter.create(settings.rate.opRateTargetPerSecond);

    boolean success;
    if (settings.rate.minThreads > 0)
        success = runMulti(settings.rate.auto, rateLimiter);
    else
        success = null != run(settings.command.getFactory(settings), settings.rate.threadCount,
                settings.command.count, settings.command.duration, rateLimiter, settings.command.durationUnits,
                output);

    if (success)
        output.println("END");
    else
        output.println("FAILURE");

    settings.disconnect();
}

From source file:com.kurtraschke.wmata.gtfsrealtime.services.WMATAAPIService.java

@PostConstruct
public void start() {
    _jsonMapper = new ObjectMapper();
    _jsonMapper.setPropertyNamingStrategy(PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE);
    _xmlMapper = new XmlMapper();
    _connectionManager = new BasicHttpClientConnectionManager();
    _limiter = RateLimiter.create(_apiRateLimit);

    if (_apiRateLimit > 9) {
        _log.warn("API rate limit set to {}, greater than default rate limit of 9 queries/second");
    }/*from   w  w w. j av  a  2 s  .  c o m*/
}

From source file:fr.gael.dhus.network.ChannelFlow.java

/**
 * @param parent the parent to set.//from ww w  .j  av a2s .c om
 */
@Override
public void setParent(Channel parent) {
    super.setParent(parent);

    UserQuotas quotas = this.getUserQuotas();

    if (quotas != null) {
        if (quotas.getMaxBandwidth() != null) {
            this.rateLimiter = RateLimiter.create(quotas.getMaxBandwidth().intValue());
        }

        if (quotas.getMaxSize() != null) {
            this.maxAllowedPermits = quotas.getMaxSize().longValue();
        }
    }
}

From source file:com.example.app.url.shortener.service.URLShortenerService.java

String tryURLShortenOperation(Function<Urlshortener, String> operation, double rate) throws IOException {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    final Urlshortener shortener = createURLShortener();
    final RateLimiter rateLimiter = RateLimiter.create(rate);
    IOException lastException = null;
    int retries = 0;
    while (retries++ < 5) {
        try {/*  ww w.  ja va2 s  . co  m*/
            return operation.apply(shortener);
        } catch (RuntimeException e) {
            if (e.getCause() instanceof IOException)
                lastException = (IOException) e.getCause();
            if (e.getCause() instanceof InterruptedIOException) {
                if (_logger.isDebugEnabled()) {
                    _logger.debug("Could not shorten URL due to connection error. Retry#" + retries
                            + ". Time spent: " + stopwatch, e);
                }
                rateLimiter.acquire();
            } else
                throw e;
        }
    }
    if (lastException != null)
        throw lastException;
    else
        throw new IOException("Unable to shorten URL. Retried " + retries + " times for " + stopwatch);
}

From source file:com.liferay.sync.engine.session.rate.limiter.RateLimiterManager.java

protected static RateLimiter getRateLimiter(long syncAccountId, Map<Long, List<RateLimiter>> rateLimiterMap) {

    RateLimiter rateLimiter = RateLimiter.create(1);

    List<RateLimiter> rateLimiters = rateLimiterMap.get(syncAccountId);

    if (rateLimiters == null) {
        rateLimiters = new ArrayList<>();

        rateLimiters.add(rateLimiter);//from w w w .java  2  s  .c o m

        rateLimiterMap.put(syncAccountId, rateLimiters);
    } else {
        rateLimiters.add(rateLimiter);
    }

    return rateLimiter;
}

From source file:api.QueryManager.java

private QueryManager() {
    client = new OkHttpClient();

    longRateLimiter = RateLimiter.create(DEFAULT_LONG_RATE_LIMIT);
    shortRateLimiter = RateLimiter.create(DEFAULT_SHORT_RATE_LIMIT);

    // Default endpoint to NA
    endpoint = endpoints.get(Region.NA);

    // Default region to NA
    region = Region.NA;//from   ww w. jav a2s.  c  o m
}

From source file:com.spotify.heroic.shell.task.MetadataLoad.java

@Override
public AsyncFuture<Void> run(final ShellIO io, TaskParameters base) throws Exception {
    final Parameters params = (Parameters) base;

    final SuggestBackend target = suggest.useGroup(params.target);

    final Optional<RateLimiter> rateLimiter = params.rate <= 0 ? Optional.<RateLimiter>absent()
            : Optional.of(RateLimiter.create(params.rate));

    io.out().println("Loading suggest data:");
    io.out().println("  from (file): " + params.file);
    io.out().println("  to  (suggest): " + target);
    io.out().println("  rate-limit:" + (rateLimiter.isPresent() ? params.rate : "disabled"));
    io.out().flush();/*from  w  w w .  j  ava 2  s  .  c o m*/

    long total = 0;
    long failed = 0;
    long ratePosition = 0;
    long rateStart = clock.currentTimeMillis();

    final DateRange now = DateRange.now(clock);

    try (final BufferedReader input = new BufferedReader(open(io, params.file))) {
        String line;

        while ((line = input.readLine()) != null) {
            if (rateLimiter.isPresent()) {
                rateLimiter.get().acquire();
            }

            final Series series = mapper.readValue(line, Series.class);

            if (rateLimiter.isPresent()) {
                rateLimiter.get().acquire();
            }

            total++;

            try {
                target.write(new WriteSuggest.Request(series, now)).get();
            } catch (Exception e) {
                failed++;
            }

            if (total % OUTPUT_STEP == 0) {
                if (failed > 0) {
                    io.out().print('!');
                    failed = 0;
                } else {
                    io.out().print('#');
                }

                if (total % (OUTPUT_STEP * 20) == 0) {
                    long rateNow = clock.currentTimeMillis();
                    final long rate;

                    if (rateNow == rateStart) {
                        rate = -1;
                    } else {
                        rate = ((total - ratePosition) * 1000) / (rateNow - rateStart);
                    }

                    io.out().println(String.format(" %d (%s/s)", total, rate == -1 ? "infinite" : rate));
                    ratePosition = total;
                    rateStart = rateNow;
                }

                io.out().flush();
            }
        }
    }

    io.out().println();
    io.out().println("Allegedly successful writes: " + (total - failed));
    io.out().println("Allegedly failed writes: " + failed);
    io.out().flush();

    return async.resolved();
}