-
-
Notifications
You must be signed in to change notification settings - Fork 20
GH-1153 Rework delay system with per-entry Instant TTL #1184
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
imDMK
wants to merge
6
commits into
master
Choose a base branch
from
improve-delay-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−25
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c63785
delay API: rework delay system with per-entry Instant TTL
imDMK 7de9479
Follow GEMINI code review
imDMK 4d0f3be
Update eternalcore-api/src/main/java/com/eternalcode/core/delay/Guava…
imDMK c9c0cae
Update eternalcore-api/src/main/java/com/eternalcode/core/delay/Guava…
imDMK b67ef43
Update eternalcore-api/src/main/java/com/eternalcode/core/delay/Delay…
imDMK 37def40
review
Rollczi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
39 changes: 39 additions & 0 deletions
39
eternalcore-core/src/main/java/com/eternalcode/core/delay/InstantExpiry.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.eternalcode.core.delay; | ||
|
||
import com.github.benmanes.caffeine.cache.Expiry; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
class InstantExpiry<T> implements Expiry<@NotNull T, @NotNull Instant> { | ||
|
||
@Override | ||
public long expireAfterCreate(T key, Instant expireTime, long currentTime) { | ||
return timeToExpire(expireTime); | ||
} | ||
|
||
@Override | ||
public long expireAfterUpdate(T key, Instant newExpireTime, long currentTime, long currentDuration) { | ||
return timeToExpire(newExpireTime); | ||
} | ||
|
||
@Override | ||
public long expireAfterRead(T key, Instant value, long currentTime, long currentDuration) { | ||
return currentDuration; | ||
} | ||
|
||
private static long timeToExpire(Instant expireTime) { | ||
Duration durationToExpire = Duration.between(Instant.now(), expireTime); | ||
if (durationToExpire.isNegative()) { | ||
return 0; | ||
} | ||
|
||
long nanos = durationToExpire.toNanos(); | ||
if (nanos == 0) { | ||
return 1; | ||
} | ||
|
||
return nanos; | ||
} | ||
|
||
} |
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
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
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
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
125 changes: 125 additions & 0 deletions
125
eternalcore-core/src/test/java/com/eternalcode/core/delay/DelayTest.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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.eternalcode.core.delay; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
import java.util.UUID; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
class DelayTest { | ||
|
||
@Test | ||
void shouldExpireAfterDefaultDelay() { | ||
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); | ||
UUID key = UUID.randomUUID(); | ||
|
||
delay.markDelay(key); | ||
assertThat(delay.hasDelay(key)).isTrue(); | ||
|
||
await() | ||
.pollDelay(250, MILLISECONDS) | ||
.atMost(500, MILLISECONDS) | ||
.until(() -> delay.hasDelay(key)); | ||
|
||
await() | ||
.atMost(Duration.ofMillis(350)) // After previously await (600 ms - 900 ms) | ||
.until(() -> !delay.hasDelay(key)); | ||
} | ||
|
||
@Test | ||
void shouldDoNotExpireBeforeCustomDelay() { | ||
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); | ||
UUID key = UUID.randomUUID(); | ||
|
||
delay.markDelay(key, Duration.ofMillis(1000)); | ||
assertThat(delay.hasDelay(key)).isTrue(); | ||
|
||
await() | ||
.pollDelay(500, MILLISECONDS) | ||
.atMost(1000, MILLISECONDS) | ||
.until(() -> delay.hasDelay(key)); | ||
|
||
await() | ||
.atMost(600, MILLISECONDS) // After previously await (1100 ms - 1600 ms) | ||
.until(() -> !delay.hasDelay(key)); | ||
} | ||
|
||
@Test | ||
void shouldUnmarkDelay() { | ||
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); | ||
UUID key = UUID.randomUUID(); | ||
|
||
delay.markDelay(key); | ||
assertThat(delay.hasDelay(key)).isTrue(); | ||
|
||
delay.unmarkDelay(key); | ||
assertThat(delay.hasDelay(key)).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldNotHaveDelayOnNonExistentKey() { | ||
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); | ||
UUID key = UUID.randomUUID(); | ||
|
||
assertThat(delay.hasDelay(key)).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldReturnCorrectRemainingTime() { | ||
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); | ||
UUID key = UUID.randomUUID(); | ||
|
||
delay.markDelay(key, Duration.ofMillis(1000)); | ||
|
||
// Immediately after marking, remaining time should be close to the full delay | ||
assertThat(delay.getRemaining(key)).isCloseTo(Duration.ofMillis(1000), Duration.ofMillis(150)); | ||
|
||
// Wait for some time | ||
await() | ||
.pollDelay(400, MILLISECONDS) | ||
.atMost(550, MILLISECONDS) | ||
.untilAsserted(() -> { | ||
// After 400ms, remaining time should be less than the original | ||
assertThat(delay.getRemaining(key)).isLessThan(Duration.ofMillis(1000).minus(Duration.ofMillis(300))); | ||
}); | ||
|
||
await() | ||
.atMost(Duration.ofMillis(1000).plus(Duration.ofMillis(150))) | ||
.until(() -> !delay.hasDelay(key)); | ||
|
||
// After expiration, remaining time should be negative | ||
assertThat(delay.getRemaining(key)).isNegative(); | ||
} | ||
|
||
@Test | ||
void shouldHandleMultipleKeysIndependently() { | ||
Delay<UUID> delay = Delay.withDefault(() -> Duration.ofMillis(500)); | ||
UUID shortTimeKey = UUID.randomUUID(); // 500ms | ||
UUID longTimeKey = UUID.randomUUID(); // 1000ms | ||
|
||
delay.markDelay(shortTimeKey); | ||
delay.markDelay(longTimeKey, Duration.ofMillis(1000)); | ||
|
||
assertThat(delay.hasDelay(shortTimeKey)).isTrue(); | ||
assertThat(delay.hasDelay(longTimeKey)).isTrue(); | ||
|
||
// Wait for the first key to expire | ||
await() | ||
.atMost(Duration.ofMillis(500).plus(Duration.ofMillis(150))) | ||
.until(() -> !delay.hasDelay(shortTimeKey)); | ||
|
||
// After first key expires, second should still be active | ||
assertThat(delay.hasDelay(shortTimeKey)).isFalse(); | ||
assertThat(delay.hasDelay(longTimeKey)).isTrue(); | ||
|
||
// Wait for the second key to expire | ||
await() | ||
.atMost(Duration.ofMillis(1000)) | ||
.until(() -> !delay.hasDelay(longTimeKey)); | ||
|
||
assertThat(delay.hasDelay(longTimeKey)).isFalse(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tutaj musi być provider
() ->
bo pobieramy wartość z configu a ona się reloaduje czasem