| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | SmoothRateLimiter.
This implements a "bursty" RateLimiter, where storedPermits are translated to zero throttling. |
| pack-priv static class | SmoothRateLimiter.
This implements the following function where coldInterval = coldFactor * stableInterval. |
| Modifier and Type | Field 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. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method 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. |
| pack-priv abstract void | |
| pack-priv final long | queryEarliestAvailable(long nowMicros)
Implements abstract com. |
| pack-priv final long | reserveEarliestAvailable(int requiredPermits, long nowMicros)
Implements abstract com. |
| pack-priv void | |
| 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. |
| maxPermits | back to summary |
|---|---|
| pack-priv double maxPermits The maximum number of stored permits. | |
| nextFreeTicketMicros | back 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. | |
| stableIntervalMicros | back 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. | |
| storedPermits | back to summary |
|---|---|
| pack-priv double storedPermits The currently stored permits. | |
| SmoothRateLimiter | back to summary |
|---|---|
| private SmoothRateLimiter(RateLimiter. | |
| coolDownIntervalMicros | back 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. | |
| doGetRate | back to summary |
|---|---|
| pack-priv final double doGetRate() Implements abstract com.
| |
| doSetRate | back to summary |
|---|---|
| pack-priv final void doSetRate(double permitsPerSecond, long nowMicros) Implements abstract com.
| |
| doSetRate | back to summary |
|---|---|
| pack-priv abstract void doSetRate(double permitsPerSecond, double stableIntervalMicros) | |
| queryEarliestAvailable | back to summary |
|---|---|
| pack-priv final long queryEarliestAvailable(long nowMicros) Implements abstract com. Doc from com. Returns the earliest time that permits are available (with one caveat).
| |
| reserveEarliestAvailable | back to summary |
|---|---|
| pack-priv final long reserveEarliestAvailable(int requiredPermits, long nowMicros) Implements abstract com. Doc from com. Reserves the requested number of permits and returns the time that those permits can be used (with one caveat).
| |
| resync | back to summary |
|---|---|
| pack-priv void resync(long nowMicros) Updates | |
| storedPermitsToWaitTime | back 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: | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final double | maxBurstSeconds
The work (permits) of how many seconds can be saved up if this RateLimiter is unused? |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv double | coolDownIntervalMicros()
Implements abstract com. |
| pack-priv void | doSetRate(double permitsPerSecond, double stableIntervalMicros)
Implements abstract com. |
| pack-priv long | storedPermitsToWaitTime(double storedPermits, double permitsToTake)
Implements abstract com. |
| maxBurstSeconds | back to summary |
|---|---|
| pack-priv final double maxBurstSeconds The work (permits) of how many seconds can be saved up if this RateLimiter is unused? | |
| SmoothBursty | back to summary |
|---|---|
| pack-priv SmoothBursty(RateLimiter. | |
| coolDownIntervalMicros | back to summary |
|---|---|
| pack-priv double coolDownIntervalMicros() Implements abstract com. Doc from com. Returns the number of microseconds during cool down that we have to wait to get a new permit.
| |
| doSetRate | back to summary |
|---|---|
| pack-priv void doSetRate(double permitsPerSecond, double stableIntervalMicros) Implements abstract com.
| |
| storedPermitsToWaitTime | back to summary |
|---|---|
| pack-priv long storedPermitsToWaitTime(double storedPermits, double permitsToTake) Implements abstract com. Doc from com. 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:
| |
^ 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:
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.
thresholdPermits = 0.5 * warmupPeriod / stableInterval
maxPermits = thresholdPermits + 2 * warmupPeriod / (stableInterval + coldInterval)
| Modifier and Type | Field 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 |
| Access | Constructor and Description |
|---|---|
| pack-priv | SmoothWarmingUp(RateLimiter.
|
| Modifier and Type | Method and Description |
|---|---|
| pack-priv double | coolDownIntervalMicros()
Implements abstract com. |
| pack-priv void | doSetRate(double permitsPerSecond, double stableIntervalMicros)
Implements abstract com. |
| private double | |
| pack-priv long | storedPermitsToWaitTime(double storedPermits, double permitsToTake)
Implements abstract com. |
| coldFactor | back to summary |
|---|---|
| private double coldFactor | |
| slope | back to summary |
|---|---|
| private double slope The slope of the line from the stable interval (when permits == 0), to the cold interval (when permits == maxPermits) | |
| thresholdPermits | back to summary |
|---|---|
| private double thresholdPermits | |
| warmupPeriodMicros | back to summary |
|---|---|
| private final long warmupPeriodMicros | |
| SmoothWarmingUp | back to summary |
|---|---|
| pack-priv SmoothWarmingUp(RateLimiter. | |
| coolDownIntervalMicros | back to summary |
|---|---|
| pack-priv double coolDownIntervalMicros() Implements abstract com. Doc from com. Returns the number of microseconds during cool down that we have to wait to get a new permit.
| |
| doSetRate | back to summary |
|---|---|
| pack-priv void doSetRate(double permitsPerSecond, double stableIntervalMicros) Implements abstract com.
| |
| permitsToTime | back to summary |
|---|---|
| private double permitsToTime(double permits) | |
| storedPermitsToWaitTime | back to summary |
|---|---|
| pack-priv long storedPermitsToWaitTime(double storedPermits, double permitsToTake) Implements abstract com. Doc from com. 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:
| |