FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: report remaining, not elapsed, time from LinearRateLimiter by csviri · Pull Request #3521 · operator-framework/java-operator-sdk · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
      • LinearRateLimiter.java
      • LinearRateLimiterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ public Optional<Duration> isLimited(RateLimitState rateLimitState) {
if (!isActivated() || !(rateLimitState instanceof RateState actualState)) {
return Optional.empty();
}

var now = LocalDateTime.now();
if (actualState.getCount() < limitForPeriod) {
actualState.increaseCount();
return Optional.empty();
} else if (actualState
.getLastRefreshTime()
.isBefore(LocalDateTime.now().minus(refreshPeriod))) {
} else if (actualState.getLastRefreshTime().isBefore(now.minus(refreshPeriod))) {
actualState.reset();
actualState.increaseCount();
return Optional.empty();
} else {
return Optional.of(Duration.between(actualState.getLastRefreshTime(), LocalDateTime.now()));
var remaining = Duration.between(now, actualState.getLastRefreshTime().plus(refreshPeriod));
return Optional.of(remaining.isNegative() ? Duration.ZERO : remaining);
}
}

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@ void returnsMinimalDurationToAcquirePermission() {
res = rl.isLimited(state);

assertThat(res).isPresent();
assertThat(res.get()).isLessThan(REFRESH_PERIOD);
assertThat(res.get()).isLessThanOrEqualTo(REFRESH_PERIOD);
// the whole period is still ahead of us, so the reported wait must be close to it
assertThat(res.get()).isGreaterThan(REFRESH_PERIOD.dividedBy(2));
}

@Test
void reportedDurationIsTheTimeRemainingNotTheTimeElapsed() throws InterruptedException {
var rl = new LinearRateLimiter(REFRESH_PERIOD, 1);
assertThat(rl.isLimited(state)).isEmpty();

var justAfterLimit = rl.isLimited(state).orElseThrow();
Thread.sleep(REFRESH_PERIOD.toMillis() / 2);
var halfWayThroughPeriod = rl.isLimited(state).orElseThrow();

// as the refresh period elapses, less time remains until a permission is available again
assertThat(halfWayThroughPeriod).isLessThan(justAfterLimit);
}

@Test
Expand Down
Loading

Back | FazBrowse Home | New Git URL