Skip to content

Navigation Menu

Sign in
Sign up

Handle zero warmup periods in RateLimiter #8586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lcy19930619 wants to merge 2 commits into google:master
base: master
Choose a base branch
Loading
from lcy19930619:codex/fix-zero-warmup-rate-limiter
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.common.testing.NullPointerTester.Visibility;
import com.google.common.util.concurrent.RateLimiter.SleepingStopwatch;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -154,6 +155,32 @@ public void testCreateWarmupParameterValidation() {
assertThrows(IllegalArgumentException.class, () -> RateLimiter.create(1.0, -1, NANOSECONDS));
}

public void testZeroWarmupIsEquivalentToNoWarmup() {
RateLimiter limiter = RateLimiter.create(5.0, 0, SECONDS, 3.0, stopwatch);
limiter.acquire();
limiter.acquire();
limiter.acquire();
assertEvents("R0.00", "R0.20", "R0.20");
}

public void testZeroDurationWarmupIsEquivalentToNoWarmup() {
RateLimiter limiter = RateLimiter.create(Double.MIN_VALUE, Duration.ZERO);
assertTrue(limiter.tryAcquire());
assertFalse(limiter.tryAcquire());
}

public void testSubMicrosecondWarmupIsEquivalentToNoWarmup() {
RateLimiter limiter = RateLimiter.create(Double.MIN_VALUE, 1, NANOSECONDS);
assertTrue(limiter.tryAcquire());
assertFalse(limiter.tryAcquire());
}

public void testSubMicrosecondDurationWarmupIsEquivalentToNoWarmup() {
RateLimiter limiter = RateLimiter.create(Double.MIN_VALUE, Duration.ofNanos(1));
assertTrue(limiter.tryAcquire());
assertFalse(limiter.tryAcquire());
}

@AndroidIncompatible // difference in String.format rounding?
public void testWarmUp() {
RateLimiter limiter = RateLimiter.create(2.0, 4000, MILLISECONDS, 3.0, stopwatch);
Expand Down
9 changes: 9 additions & 0 deletions guava/src/com/google/common/util/concurrent/RateLimiter.java
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ static RateLimiter create(double permitsPerSecond, SleepingStopwatch stopwatch)
* <p>The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period will
* follow), and if it is left unused for long enough, it will return to that state.
*
* <p>A {@code warmupPeriod} of zero is equivalent to {@link #create(double)}.
*
* @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in how many
* permits become available per second
* @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its rate,
Expand Down Expand Up @@ -184,6 +186,8 @@ public static RateLimiter create(double permitsPerSecond, Duration warmupPeriod)
* <p>The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period will
* follow), and if it is left unused for long enough, it will return to that state.
*
* <p>A {@code warmupPeriod} of zero is equivalent to {@link #create(double)}.
*
* @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in how many
* permits become available per second
* @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its rate,
Expand All @@ -206,6 +210,11 @@ static RateLimiter create(
TimeUnit unit,
double coldFactor,
SleepingStopwatch stopwatch) {
checkNotNull(unit);
// SmoothWarmingUp operates in microseconds and cannot represent a shorter warmup period.
if (warmupPeriod >= 0 && unit.toMicros(warmupPeriod) == 0) {
return create(permitsPerSecond, stopwatch);
}
RateLimiter rateLimiter = new SmoothWarmingUp(stopwatch, warmupPeriod, unit, coldFactor);
rateLimiter.setRate(permitsPerSecond);
return rateLimiter;
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /