/** * Returns the stable rate (as {@code permits per seconds}) with which this {@code RateLimiter} is * configured with. The initial value of this is the same as the {@code permitsPerSecond} argument * passed in the factory method that produced this {@code RateLimiter}, and it is only updated * after invocations to {@linkplain #setRate}. */ public final double getRate() { synchronized (mutex()) { return doGetRate(); } }
/** * Returns the stable rate (as {@code permits per seconds}) with which this {@code RateLimiter} is * configured with. The initial value of this is the same as the {@code permitsPerSecond} argument * passed in the factory method that produced this {@code RateLimiter}, and it is only updated * after invocations to {@linkplain #setRate}. */ public final double getRate() { synchronized (mutex()) { return doGetRate(); } }
/** * Returns the stable rate (as {@code permits per seconds}) with which this {@code RateLimiter} is * configured with. The initial value of this is the same as the {@code permitsPerSecond} argument * passed in the factory method that produced this {@code RateLimiter}, and it is only updated * after invocations to {@linkplain #setRate}. */ public final double getRate() { synchronized (mutex()) { return doGetRate(); } }
/** * Updates the stable rate of this {@code RateLimiter}, that is, the {@code permitsPerSecond} * argument provided in the factory method that constructed the {@code RateLimiter}. Currently * throttled threads will <b>not</b> be awakened as a result of this invocation, thus they do not * observe the new rate; only subsequent requests will. * * <p>Note though that, since each request repays (by waiting, if necessary) the cost of the * <i>previous</i> request, this means that the very next request after an invocation to {@code * setRate} will not be affected by the new rate; it will pay the cost of the previous request, * which is in terms of the previous rate. * * <p>The behavior of the {@code RateLimiter} is not modified in any other way, e.g. if the {@code * RateLimiter} was configured with a warmup period of 20 seconds, it still has a warmup period of * 20 seconds after this method invocation. * * @param permitsPerSecond the new stable rate of this {@code RateLimiter} * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero */ public final void setRate(double permitsPerSecond) { checkArgument( permitsPerSecond > 0.0 && !Double.isNaN(permitsPerSecond), "rate must be positive"); synchronized (mutex()) { doSetRate(permitsPerSecond, stopwatch.readMicros()); } }
/** * Reserves the given number of permits from this {@code RateLimiter} for future use, returning * the number of microseconds until the reservation can be consumed. * * @return time in microseconds to wait until the resource can be acquired, never negative */ final long reserve(int permits) { checkPermits(permits); synchronized (mutex()) { return reserveAndGetWaitLength(permits, stopwatch.readMicros()); } }
/** * Updates the stable rate of this {@code RateLimiter}, that is, the {@code permitsPerSecond} * argument provided in the factory method that constructed the {@code RateLimiter}. Currently * throttled threads will <b>not</b> be awakened as a result of this invocation, thus they do not * observe the new rate; only subsequent requests will. * * <p>Note though that, since each request repays (by waiting, if necessary) the cost of the * <i>previous</i> request, this means that the very next request after an invocation to {@code * setRate} will not be affected by the new rate; it will pay the cost of the previous request, * which is in terms of the previous rate. * * <p>The behavior of the {@code RateLimiter} is not modified in any other way, e.g. if the {@code * RateLimiter} was configured with a warmup period of 20 seconds, it still has a warmup period of * 20 seconds after this method invocation. * * @param permitsPerSecond the new stable rate of this {@code RateLimiter} * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero */ public final void setRate(double permitsPerSecond) { checkArgument( permitsPerSecond > 0.0 && !Double.isNaN(permitsPerSecond), "rate must be positive"); synchronized (mutex()) { doSetRate(permitsPerSecond, stopwatch.readMicros()); } }
/** * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained * without exceeding the specified {@code timeout}, or returns {@code false} immediately (without * waiting) if the permits would not have been granted before the timeout expired. * * @param permits the number of permits to acquire * @param timeout the maximum time to wait for the permits. Negative values are treated as zero. * @param unit the time unit of the timeout argument * @return {@code true} if the permits were acquired, {@code false} otherwise * @throws IllegalArgumentException if the requested number of permits is negative or zero */ public boolean tryAcquire(int permits, long timeout, TimeUnit unit) { long timeoutMicros = max(unit.toMicros(timeout), 0); checkPermits(permits); long microsToWait; synchronized (mutex()) { long nowMicros = stopwatch.readMicros(); if (!canAcquire(nowMicros, timeoutMicros)) { return false; } else { microsToWait = reserveAndGetWaitLength(permits, nowMicros); } } stopwatch.sleepMicrosUninterruptibly(microsToWait); return true; }
/** * Updates the stable rate of this {@code RateLimiter}, that is, the {@code permitsPerSecond} * argument provided in the factory method that constructed the {@code RateLimiter}. Currently * throttled threads will <b>not</b> be awakened as a result of this invocation, thus they do not * observe the new rate; only subsequent requests will. * * <p>Note though that, since each request repays (by waiting, if necessary) the cost of the * <i>previous</i> request, this means that the very next request after an invocation to {@code * setRate} will not be affected by the new rate; it will pay the cost of the previous request, * which is in terms of the previous rate. * * <p>The behavior of the {@code RateLimiter} is not modified in any other way, e.g. if the {@code * RateLimiter} was configured with a warmup period of 20 seconds, it still has a warmup period of * 20 seconds after this method invocation. * * @param permitsPerSecond the new stable rate of this {@code RateLimiter} * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero */ public final void setRate(double permitsPerSecond) { checkArgument( permitsPerSecond > 0.0 && !Double.isNaN(permitsPerSecond), "rate must be positive"); synchronized (mutex()) { doSetRate(permitsPerSecond, stopwatch.readMicros()); } }
/** * Reserves the given number of permits from this {@code RateLimiter} for future use, returning * the number of microseconds until the reservation can be consumed. * * @return time in microseconds to wait until the resource can be acquired, never negative */ final long reserve(int permits) { checkPermits(permits); synchronized (mutex()) { return reserveAndGetWaitLength(permits, stopwatch.readMicros()); } }
/** * Reserves the given number of permits from this {@code RateLimiter} for future use, returning * the number of microseconds until the reservation can be consumed. * * @return time in microseconds to wait until the resource can be acquired, never negative */ final long reserve(int permits) { checkPermits(permits); synchronized (mutex()) { return reserveAndGetWaitLength(permits, stopwatch.readMicros()); } }
/** * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained * without exceeding the specified {@code timeout}, or returns {@code false} immediately (without * waiting) if the permits would not have been granted before the timeout expired. * * @param permits the number of permits to acquire * @param timeout the maximum time to wait for the permits. Negative values are treated as zero. * @param unit the time unit of the timeout argument * @return {@code true} if the permits were acquired, {@code false} otherwise * @throws IllegalArgumentException if the requested number of permits is negative or zero */ public boolean tryAcquire(int permits, long timeout, TimeUnit unit) { long timeoutMicros = max(unit.toMicros(timeout), 0); checkPermits(permits); long microsToWait; synchronized (mutex()) { long nowMicros = stopwatch.readMicros(); if (!canAcquire(nowMicros, timeoutMicros)) { return false; } else { microsToWait = reserveAndGetWaitLength(permits, nowMicros); } } stopwatch.sleepMicrosUninterruptibly(microsToWait); return true; }
/** * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained * without exceeding the specified {@code timeout}, or returns {@code false} immediately (without * waiting) if the permits would not have been granted before the timeout expired. * * @param permits the number of permits to acquire * @param timeout the maximum time to wait for the permits. Negative values are treated as zero. * @param unit the time unit of the timeout argument * @return {@code true} if the permits were acquired, {@code false} otherwise * @throws IllegalArgumentException if the requested number of permits is negative or zero */ public boolean tryAcquire(int permits, long timeout, TimeUnit unit) { long timeoutMicros = max(unit.toMicros(timeout), 0); checkPermits(permits); long microsToWait; synchronized (mutex()) { long nowMicros = stopwatch.readMicros(); if (!canAcquire(nowMicros, timeoutMicros)) { return false; } else { microsToWait = reserveAndGetWaitLength(permits, nowMicros); } } stopwatch.sleepMicrosUninterruptibly(microsToWait); return true; }
/** * Returns the stable rate (as {@code permits per seconds}) with which this {@code RateLimiter} is * configured with. The initial value of this is the same as the {@code permitsPerSecond} argument * passed in the factory method that produced this {@code RateLimiter}, and it is only updated * after invocations to {@linkplain #setRate}. */ public final double getRate() { synchronized (mutex()) { return doGetRate(); } }
/** * Returns the stable rate (as {@code permits per seconds}) with which this * {@code RateLimiter} is configured with. The initial value of this is the same as * the {@code permitsPerSecond} argument passed in the factory method that produced * this {@code RateLimiter}, and it is only updated after invocations * to {@linkplain #setRate}. */ public final double getRate() { synchronized (mutex()) { return doGetRate(); } }
/** * Returns the stable rate (as {@code permits per seconds}) with which this {@code RateLimiter} is * configured with. The initial value of this is the same as the {@code permitsPerSecond} argument * passed in the factory method that produced this {@code RateLimiter}, and it is only updated * after invocations to {@linkplain #setRate}. */ public final double getRate() { synchronized (mutex()) { return doGetRate(); } }
/** * Updates the stable rate of this {@code RateLimiter}, that is, the {@code permitsPerSecond} * argument provided in the factory method that constructed the {@code RateLimiter}. Currently * throttled threads will <b>not</b> be awakened as a result of this invocation, thus they do not * observe the new rate; only subsequent requests will. * * <p>Note though that, since each request repays (by waiting, if necessary) the cost of the * <i>previous</i> request, this means that the very next request after an invocation to {@code * setRate} will not be affected by the new rate; it will pay the cost of the previous request, * which is in terms of the previous rate. * * <p>The behavior of the {@code RateLimiter} is not modified in any other way, e.g. if the {@code * RateLimiter} was configured with a warmup period of 20 seconds, it still has a warmup period of * 20 seconds after this method invocation. * * @param permitsPerSecond the new stable rate of this {@code RateLimiter} * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero */ public final void setRate(double permitsPerSecond) { checkArgument( permitsPerSecond > 0.0 && !Double.isNaN(permitsPerSecond), "rate must be positive"); synchronized (mutex()) { doSetRate(permitsPerSecond, stopwatch.readMicros()); } }
/** * Reserves the given number of permits from this {@code RateLimiter} for future use, returning * the number of microseconds until the reservation can be consumed. * * @return time in microseconds to wait until the resource can be acquired, never negative */ final long reserve(int permits) { checkPermits(permits); synchronized (mutex()) { return reserveAndGetWaitLength(permits, stopwatch.readMicros()); } }
/** * Reserves the given number of permits from this {@code RateLimiter} for future use, returning * the number of microseconds until the reservation can be consumed. * * @return time in microseconds to wait until the resource can be acquired, never negative */ final long reserve(int permits) { checkPermits(permits); synchronized (mutex()) { return reserveAndGetWaitLength(permits, stopwatch.readMicros()); } }
/** * Reserves the given number of permits from this {@code RateLimiter} for future use, returning * the number of microseconds until the reservation can be consumed. * * @return time in microseconds to wait until the resource can be acquired, never negative */ final long reserve(int permits) { checkPermits(permits); synchronized (mutex()) { return reserveAndGetWaitLength(permits, stopwatch.readMicros()); } }
/** * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained * without exceeding the specified {@code timeout}, or returns {@code false} * immediately (without waiting) if the permits would not have been granted * before the timeout expired. * * @param permits the number of permits to acquire * @param timeout the maximum time to wait for the permits. Negative values are treated as zero. * @param unit the time unit of the timeout argument * @return {@code true} if the permits were acquired, {@code false} otherwise * @throws IllegalArgumentException if the requested number of permits is negative or zero */ public boolean tryAcquire(int permits, long timeout, TimeUnit unit) { long timeoutMicros = max(unit.toMicros(timeout), 0); checkPermits(permits); long microsToWait; synchronized (mutex()) { long nowMicros = stopwatch.readMicros(); if (!canAcquire(nowMicros, timeoutMicros)) { return false; } else { microsToWait = reserveAndGetWaitLength(permits, nowMicros); } } stopwatch.sleepMicrosUninterruptibly(microsToWait); return true; }