Top Inners Fields Constructors Methods
com.google.common.util.concurrent

pack-priv abstract Class SmoothRateLimiter

extends RateLimiter
Class Inheritance
Known Direct Subclasses
com.google.common.util.concurrent.SmoothRateLimiter.SmoothWarmingUp, com.google.common.util.concurrent.SmoothRateLimiter.SmoothBursty
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
java.lang.Math.min, java.util.concurrent.TimeUnit.SECONDS

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SmoothRateLimiter.SmoothBursty

This implements a "bursty" RateLimiter, where storedPermits are translated to zero throttling.

pack-priv static class
SmoothRateLimiter.SmoothWarmingUp

This implements the following function where coldInterval = coldFactor * stableInterval.

Field Summary

Modifier and TypeField and Description
pack-priv double
maxPermits

The maximum number of stored permits.

private long
nextFreeTicketMicros

The time when the next request (no matter its size) will be granted.

pack-priv double
stableIntervalMicros

The interval between two unit requests, at our stable rate.

pack-priv double
storedPermits

The currently stored permits.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract double
coolDownIntervalMicros()

Returns the number of microseconds during cool down that we have to wait to get a new permit.

pack-priv final double
pack-priv final void
doSetRate(double permitsPerSecond, long nowMicros)

Implements abstract com.google.common.util.concurrent.RateLimiter.doSetRate.

pack-priv abstract void
doSetRate(double permitsPerSecond, double stableIntervalMicros)

pack-priv final long
queryEarliestAvailable(long nowMicros)

Implements abstract com.google.common.util.concurrent.RateLimiter.queryEarliestAvailable.

Returns the earliest time that permits are available (with one caveat).
pack-priv final long
reserveEarliestAvailable(int requiredPermits, long nowMicros)

Implements abstract com.google.common.util.concurrent.RateLimiter.reserveEarliestAvailable.

Reserves the requested number of permits and returns the time that those permits can be used (with one caveat).
pack-priv void
resync(long nowMicros)

Updates storedPermits and nextFreeTicketMicros based on the current time.

pack-priv abstract long
storedPermitsToWaitTime(double storedPermits, double permitsToTake)

Translates a specified portion of our currently stored permits which we want to spend/acquire, into a throttling time.

Inherited from com.google.common.util.concurrent.RateLimiter:
acquireacquirecreatecreatecreatecreatecreategetRatereservereserveAndGetWaitLengthsetRatetoStringtryAcquiretryAcquiretryAcquiretryAcquiretryAcquiretryAcquire

Field Detail

maxPermitsback to summary
pack-priv double maxPermits

The maximum number of stored permits.

nextFreeTicketMicrosback to summary
private long nextFreeTicketMicros

The time when the next request (no matter its size) will be granted. After granting a request, this is pushed further in the future. Large requests push this further than small requests.

stableIntervalMicrosback to summary
pack-priv double stableIntervalMicros

The interval between two unit requests, at our stable rate. E.g., a stable rate of 5 permits per second has a stable interval of 200ms.

storedPermitsback to summary
pack-priv double storedPermits

The currently stored permits.

Constructor Detail

SmoothRateLimiterback to summary
private SmoothRateLimiter(RateLimiter.SleepingStopwatch stopwatch)

Method Detail

coolDownIntervalMicrosback to summary
pack-priv abstract double coolDownIntervalMicros()

Returns the number of microseconds during cool down that we have to wait to get a new permit.

doGetRateback to summary
pack-priv final double doGetRate()

Implements abstract com.google.common.util.concurrent.RateLimiter.doGetRate.

Annotations
@Override
doSetRateback to summary
pack-priv final void doSetRate(double permitsPerSecond, long nowMicros)

Implements abstract com.google.common.util.concurrent.RateLimiter.doSetRate.

Annotations
@Override
doSetRateback to summary
pack-priv abstract void doSetRate(double permitsPerSecond, double stableIntervalMicros)
queryEarliestAvailableback to summary
pack-priv final long queryEarliestAvailable(long nowMicros)

Implements abstract com.google.common.util.concurrent.RateLimiter.queryEarliestAvailable.

Doc from com.google.common.util.concurrent.RateLimiter.queryEarliestAvailable.

Returns the earliest time that permits are available (with one caveat).

Returns:long

the time that permits are available, or, if permits are available immediately, an arbitrary past or present time

Annotations
@Override
reserveEarliestAvailableback to summary
pack-priv final long reserveEarliestAvailable(int requiredPermits, long nowMicros)

Implements abstract com.google.common.util.concurrent.RateLimiter.reserveEarliestAvailable.

Doc from com.google.common.util.concurrent.RateLimiter.reserveEarliestAvailable.

Reserves the requested number of permits and returns the time that those permits can be used (with one caveat).

Returns:long

the time that the permits may be used, or, if the permits may be used immediately, an arbitrary past or present time

Annotations
@Override
resyncback to summary
pack-priv void resync(long nowMicros)

Updates storedPermits and nextFreeTicketMicros based on the current time.

storedPermitsToWaitTimeback to summary
pack-priv abstract long storedPermitsToWaitTime(double storedPermits, double permitsToTake)

Translates a specified portion of our currently stored permits which we want to spend/acquire, into a throttling time. Conceptually, this evaluates the integral of the underlying function we use, for the range of [(storedPermits - permitsToTake), storedPermits].

This always holds: 0 <= permitsToTake <= storedPermits

com.google.common.util.concurrent back to summary

pack-priv final Class SmoothRateLimiter.SmoothBursty

extends SmoothRateLimiter
Class Inheritance

This implements a "bursty" RateLimiter, where storedPermits are translated to zero throttling. The maximum number of permits that can be saved (when the RateLimiter is unused) is defined in terms of time, in this sense: if a RateLimiter is 2qps, and this time is specified as 10 seconds, we can save up to 2 * 10 = 20 permits.

Field Summary

Modifier and TypeField and Description
pack-priv final double
maxBurstSeconds

The work (permits) of how many seconds can be saved up if this RateLimiter is unused?

Inherited from com.google.common.util.concurrent.SmoothRateLimiter:
maxPermitsstableIntervalMicrosstoredPermits

Constructor Summary

AccessConstructor and Description
pack-priv
SmoothBursty(RateLimiter.SleepingStopwatch stopwatch, double maxBurstSeconds)

Method Summary

Modifier and TypeMethod and Description
pack-priv double
coolDownIntervalMicros()

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.coolDownIntervalMicros.

Returns the number of microseconds during cool down that we have to wait to get a new permit.
pack-priv void
doSetRate(double permitsPerSecond, double stableIntervalMicros)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.doSetRate.

pack-priv long
storedPermitsToWaitTime(double storedPermits, double permitsToTake)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.storedPermitsToWaitTime.

Translates a specified portion of our currently stored permits which we want to spend/acquire, into a throttling time.
Inherited from com.google.common.util.concurrent.SmoothRateLimiter:
doGetRatedoSetRatequeryEarliestAvailablereserveEarliestAvailableresync

Field Detail

maxBurstSecondsback to summary
pack-priv final double maxBurstSeconds

The work (permits) of how many seconds can be saved up if this RateLimiter is unused?

Constructor Detail

SmoothBurstyback to summary
pack-priv SmoothBursty(RateLimiter.SleepingStopwatch stopwatch, double maxBurstSeconds)

Method Detail

coolDownIntervalMicrosback to summary
pack-priv double coolDownIntervalMicros()

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.coolDownIntervalMicros.

Doc from com.google.common.util.concurrent.SmoothRateLimiter.coolDownIntervalMicros.

Returns the number of microseconds during cool down that we have to wait to get a new permit.

Annotations
@Override
doSetRateback to summary
pack-priv void doSetRate(double permitsPerSecond, double stableIntervalMicros)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.doSetRate.

Annotations
@Override
storedPermitsToWaitTimeback to summary
pack-priv long storedPermitsToWaitTime(double storedPermits, double permitsToTake)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.storedPermitsToWaitTime.

Doc from com.google.common.util.concurrent.SmoothRateLimiter.storedPermitsToWaitTime.

Translates a specified portion of our currently stored permits which we want to spend/acquire, into a throttling time. Conceptually, this evaluates the integral of the underlying function we use, for the range of [(storedPermits - permitsToTake), storedPermits].

This always holds: 0 <= permitsToTake <= storedPermits

Annotations
@Override
com.google.common.util.concurrent back to summary

pack-priv final Class SmoothRateLimiter.SmoothWarmingUp

extends SmoothRateLimiter
Class Inheritance

This implements the following function where coldInterval = coldFactor * stableInterval.
         ^ throttling
         |
   cold  +                  /
interval |                 /.
         |                / .
         |               /  .   ← "warmup period" is the area of the trapezoid between
         |              /   .     thresholdPermits and maxPermits
         |             /    .
         |            /     .
         |           /      .
  stable +----------/  WARM .
interval |          .   UP  .
         |          . PERIOD.
         |          .       .
       0 +----------+-------+--------------→ storedPermits
         0 thresholdPermits maxPermits
Before going into the details of this particular function, let's keep in mind the basics:
  1. The state of the RateLimiter (storedPermits) is a vertical line in this figure.
  2. When the RateLimiter is not used, this goes right (up to maxPermits)
  3. When the RateLimiter is used, this goes left (down to zero), since if we have storedPermits, we serve from those first
  4. When _unused_, we go right at a constant rate! The rate at which we move to the right is chosen as maxPermits / warmupPeriod. This ensures that the time it takes to go from 0 to maxPermits is equal to warmupPeriod.
  5. When _used_, the time it takes, as explained in the introductory class note, is equal to the integral of our function, between X permits and X-K permits, assuming we want to spend K saved permits.

In summary, the time it takes to move to the left (spend K permits), is equal to the area of the function of width == K.

Assuming we have saturated demand, the time to go from maxPermits to thresholdPermits is equal to warmupPeriod. And the time to go from thresholdPermits to 0 is warmupPeriod/2. (The reason that this is warmupPeriod/2 is to maintain the behavior of the original implementation where coldFactor was hard coded as 3.)

It remains to calculate thresholdsPermits and maxPermits.

Field Summary

Modifier and TypeField and Description
private double
private double
slope

The slope of the line from the stable interval (when permits == 0), to the cold interval (when permits == maxPermits)

private double
private final long
Inherited from com.google.common.util.concurrent.SmoothRateLimiter:
maxPermitsstableIntervalMicrosstoredPermits

Constructor Summary

AccessConstructor and Description
pack-priv
SmoothWarmingUp(RateLimiter.SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor)

Method Summary

Modifier and TypeMethod and Description
pack-priv double
coolDownIntervalMicros()

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.coolDownIntervalMicros.

Returns the number of microseconds during cool down that we have to wait to get a new permit.
pack-priv void
doSetRate(double permitsPerSecond, double stableIntervalMicros)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.doSetRate.

private double
permitsToTime(double permits)

pack-priv long
storedPermitsToWaitTime(double storedPermits, double permitsToTake)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.storedPermitsToWaitTime.

Translates a specified portion of our currently stored permits which we want to spend/acquire, into a throttling time.
Inherited from com.google.common.util.concurrent.SmoothRateLimiter:
doGetRatedoSetRatequeryEarliestAvailablereserveEarliestAvailableresync

Field Detail

coldFactorback to summary
private double coldFactor
slopeback to summary
private double slope

The slope of the line from the stable interval (when permits == 0), to the cold interval (when permits == maxPermits)

thresholdPermitsback to summary
private double thresholdPermits
warmupPeriodMicrosback to summary
private final long warmupPeriodMicros

Constructor Detail

SmoothWarmingUpback to summary
pack-priv SmoothWarmingUp(RateLimiter.SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor)

Method Detail

coolDownIntervalMicrosback to summary
pack-priv double coolDownIntervalMicros()

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.coolDownIntervalMicros.

Doc from com.google.common.util.concurrent.SmoothRateLimiter.coolDownIntervalMicros.

Returns the number of microseconds during cool down that we have to wait to get a new permit.

Annotations
@Override
doSetRateback to summary
pack-priv void doSetRate(double permitsPerSecond, double stableIntervalMicros)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.doSetRate.

Annotations
@Override
permitsToTimeback to summary
private double permitsToTime(double permits)
storedPermitsToWaitTimeback to summary
pack-priv long storedPermitsToWaitTime(double storedPermits, double permitsToTake)

Implements abstract com.google.common.util.concurrent.SmoothRateLimiter.storedPermitsToWaitTime.

Doc from com.google.common.util.concurrent.SmoothRateLimiter.storedPermitsToWaitTime.

Translates a specified portion of our currently stored permits which we want to spend/acquire, into a throttling time. Conceptually, this evaluates the integral of the underlying function we use, for the range of [(storedPermits - permitsToTake), storedPermits].

This always holds: 0 <= permitsToTake <= storedPermits

Annotations
@Override