-
-
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
8
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.
Open
Changes from 7 commits
Commits
Show all changes
8 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 f9b8236
Added Javadocs and introduced configurable maximumSize value.
imDMK 6674328
Follow Rollczi review suggestions.
imDMK 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
50 changes: 0 additions & 50 deletions
50
eternalcore-api/src/main/java/com/eternalcode/core/delay/Delay.java
This file was deleted.
Oops, something went wrong.
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
136 changes: 136 additions & 0 deletions
136
eternalcore-core/src/main/java/com/eternalcode/core/delay/Delay.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,136 @@ | ||
| package com.eternalcode.core.delay; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Provides per-entry delay management using Caffeine with wall-clock based expiration. | ||
| * | ||
| * @param <T> the key type | ||
| */ | ||
| public class Delay<T> { | ||
|
|
||
| private static final long DEFAULT_MAXIMUM_SIZE = 50_000L; | ||
|
|
||
| private final Cache<T, Instant> cache; | ||
| private final Supplier<Duration> defaultDelay; | ||
|
|
||
| /** | ||
| * Creates a new delay with a default delay supplier and cache size limit. | ||
| * | ||
| * @param defaultDelay supplier providing default delay durations | ||
| * @param maximumSize maximum number of cached entries | ||
| */ | ||
| private Delay(Supplier<Duration> defaultDelay, long maximumSize) { | ||
| if (defaultDelay == null) { | ||
| throw new IllegalArgumentException("defaultDelay cannot be null"); | ||
| } | ||
|
|
||
| if (maximumSize <= 0) { | ||
| throw new IllegalArgumentException("maximumSize must be > 0"); | ||
| } | ||
|
|
||
| this.defaultDelay = defaultDelay; | ||
| this.cache = Caffeine.newBuilder() | ||
| .maximumSize(maximumSize) | ||
imDMK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .expireAfter(new InstantExpiry<T>()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new delay manager with the default maximum cache size. | ||
| * | ||
| * @param defaultDelay supplier providing default delay durations | ||
| */ | ||
| private Delay(Supplier<Duration> defaultDelay) { | ||
| this(defaultDelay, DEFAULT_MAXIMUM_SIZE); | ||
| } | ||
|
|
||
| /** | ||
| * Marks a delay for the given key using a specific duration. | ||
| * | ||
| * @param key the key to delay | ||
| * @param delay the duration of the delay | ||
| */ | ||
| public void markDelay(T key, Duration delay) { | ||
| if (delay.isZero() || delay.isNegative()) { | ||
| this.cache.invalidate(key); | ||
| } | ||
|
|
||
| this.cache.put(key, Instant.now().plus(delay)); | ||
| } | ||
|
|
||
| /** | ||
| * Marks a delay for the given key using the default duration. | ||
| * | ||
| * @param key the key to delay | ||
| */ | ||
| public void markDelay(T key) { | ||
| this.markDelay(key, this.defaultDelay.get()); | ||
| } | ||
|
|
||
| /** | ||
| * Removes any existing delay for the given key. | ||
| * | ||
| * @param key the key to clear | ||
| */ | ||
| public void unmarkDelay(T key) { | ||
| this.cache.invalidate(key); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the given key currently has an active delay. | ||
| * | ||
| * @param key the key to check | ||
| * @return true if the delay is active, false otherwise | ||
| */ | ||
| public boolean hasDelay(T key) { | ||
| Instant delayExpireMoment = this.getExpireAt(key); | ||
| return Instant.now().isBefore(delayExpireMoment); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the remaining delay duration for the given key. | ||
| * | ||
| * @param key the key to check | ||
| * @return the remaining duration, or {@code Duration.ZERO} if expired | ||
| */ | ||
| public Duration getRemaining(T key) { | ||
| return Duration.between(Instant.now(), this.getExpireAt(key)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the expiration instant for the given key. | ||
| * | ||
| * @param key the key to check | ||
| * @return the expiration instant, or {@code Instant.MIN} if none | ||
| */ | ||
| private Instant getExpireAt(T key) { | ||
| return this.cache.asMap().getOrDefault(key, Instant.MIN); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link Delay} instance with a default delay supplier. | ||
| * | ||
| * @param defaultDelay supplier providing default delay durations | ||
| * @return a new Delay instance | ||
| */ | ||
| public static <T> Delay<T> withDefault(Supplier<Duration> defaultDelay) { | ||
| return new Delay<>(defaultDelay); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new {@link Delay} instance with a default delay supplier and cache size. | ||
| * | ||
| * @param defaultDelay supplier providing default delay durations | ||
| * @param maximumSize maximum number of cached entries | ||
| * @return a new Delay instance | ||
| */ | ||
| public static <T> Delay<T> withDefault(Supplier<Duration> defaultDelay, long maximumSize) { | ||
| return new Delay<>(defaultDelay, maximumSize); | ||
| } | ||
| } | ||
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(@NotNull T key, @NotNull Instant expireTime, long currentTime) { | ||
| return timeToExpire(expireTime); | ||
| } | ||
|
|
||
| @Override | ||
| public long expireAfterUpdate(@NotNull T key, @NotNull Instant newExpireTime, long currentTime, long currentDuration) { | ||
| return timeToExpire(newExpireTime); | ||
| } | ||
|
|
||
| @Override | ||
| public long expireAfterRead(@NotNull T key, @NotNull Instant value, long currentTime, long currentDuration) { | ||
| return currentDuration; | ||
| } | ||
|
|
||
| private static long timeToExpire(Instant expireTime) { | ||
| Duration toExpire = Duration.between(Instant.now(), expireTime); | ||
| if (toExpire.isNegative()) { | ||
| return 0; | ||
| } | ||
|
|
||
| long nanos = toExpire.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ class RandomTeleportCommand { | |
| this.randomTeleportService = randomTeleportService; | ||
| this.randomTeleportTaskService = randomTeleportTaskService; | ||
| this.randomTeleportSettings = randomTeleportSettings; | ||
| this.cooldown = new Delay<>(() -> this.randomTeleportSettings.cooldown()); | ||
| this.cooldown = Delay.withDefault(() -> this.randomTeleportSettings.cooldown()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tutaj musi być provider |
||
| } | ||
|
|
||
| @Execute | ||
|
|
@@ -68,7 +68,7 @@ void executeSelf(@Sender Player player) { | |
| this.handleTeleportSuccess(player); | ||
| }); | ||
|
|
||
| this.cooldown.markDelay(uuid, this.randomTeleportSettings.cooldown()); | ||
| this.cooldown.markDelay(uuid); | ||
| } | ||
|
|
||
| @Execute | ||
|
|
@@ -96,7 +96,7 @@ void executeOther(@Sender Viewer sender, @Arg Player player) { | |
| this.handleAdminTeleport(sender, player); | ||
| }); | ||
|
|
||
| this.cooldown.markDelay(uuid, this.randomTeleportSettings.cooldown()); | ||
| this.cooldown.markDelay(uuid); | ||
| } | ||
|
|
||
| private void handleTeleportSuccess(Player player) { | ||
|
|
@@ -129,7 +129,7 @@ private boolean hasRandomTeleportDelay(Player player) { | |
| } | ||
|
|
||
| if (this.cooldown.hasDelay(uniqueId)) { | ||
| Duration time = this.cooldown.getDurationToExpire(uniqueId); | ||
| Duration time = this.cooldown.getRemaining(uniqueId); | ||
|
|
||
| this.noticeService.create() | ||
| .notice(translation -> translation.randomTeleport().randomTeleportDelay()) | ||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.