-
-
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
5
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.
+411
−68
Open
Changes from 1 commit
Commits
Show all changes
5 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 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
eternalcore-api/src/main/java/com/eternalcode/core/delay/DefaultDelay.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,63 @@ | ||
package com.eternalcode.core.delay; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
/** | ||
* Represents a delay mechanism with a predefined default duration. | ||
* <p> | ||
* A key can be marked either with the default duration or with a custom one. | ||
* | ||
* @param <T> the type of key used to identify the delay | ||
*/ | ||
public interface DefaultDelay<T> { | ||
|
||
/** | ||
* Marks a delay for the given key using the predefined default duration. | ||
* | ||
* @param key the key to mark | ||
*/ | ||
void markDelay(T key); | ||
|
||
/** | ||
* Removes any existing delay for the given key. | ||
* | ||
* @param key the key to unmark | ||
*/ | ||
void unmarkDelay(T key); | ||
|
||
/** | ||
* Checks if the given key currently has an active delay. | ||
* | ||
* @param key the key to check | ||
* @return true if the key has a delay, false otherwise | ||
*/ | ||
boolean hasDelay(T key); | ||
|
||
/** | ||
* Returns the remaining duration of the delay for the given key. | ||
* Returns {@code Duration.ZERO} if no active delay exists. | ||
* | ||
* @param key the key to check | ||
* @return remaining duration, or {@code Duration.ZERO} if none | ||
*/ | ||
Duration getRemaining(T key); | ||
|
||
/** | ||
* Returns the expiration time of the delay for the given key. | ||
* Returns {@code null} if no active delay exists. | ||
* | ||
* @param key the key to check | ||
* @return expiration instant, or {@code null} if none | ||
*/ | ||
Instant getExpireAt(T key); | ||
|
||
/** | ||
* Extends the delay for the given key by the specified extra duration. | ||
* If no active delay exists, a new one is created starting now. | ||
* | ||
* @param key the key to extend | ||
* @param extra the duration to add | ||
*/ | ||
void extendDelay(T key, Duration extra); | ||
imDMK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
83 changes: 47 additions & 36 deletions
83
eternalcore-api/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 |
---|---|---|
@@ -1,50 +1,61 @@ | ||
package com.eternalcode.core.delay; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.function.Supplier; | ||
|
||
public class Delay<T> { | ||
|
||
private final Cache<T, Instant> delays; | ||
|
||
private final Supplier<Duration> delaySettings; | ||
|
||
public Delay(Supplier<Duration> delayProvider) { | ||
this.delaySettings = delayProvider; | ||
|
||
this.delays = CacheBuilder.newBuilder() | ||
.expireAfterWrite(delayProvider.get()) | ||
.build(); | ||
/** | ||
* Factory class for creating delay instances. | ||
* <p> | ||
* Naming convention: | ||
* - withDefault(...) -> DefaultDelay (uses predefined Duration) | ||
* - explicit(...) -> ExplicitDelay (requires explicit Duration each time) | ||
*/ | ||
public final class Delay { | ||
|
||
private Delay() { | ||
throw new UnsupportedOperationException("Utility class - do not instantiate"); | ||
imDMK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
public void markDelay(T key, Duration delay) { | ||
this.delays.put(key, Instant.now().plus(delay)); | ||
/** | ||
* Creates a DefaultDelay with the default cache size. | ||
* | ||
* @param defaultDelay the default duration used for delays | ||
* @param <T> the key type | ||
* @return DefaultDelay instance | ||
*/ | ||
public static <T> DefaultDelay<T> withDefault(Duration defaultDelay) { | ||
return new GuavaDefaultDelay<>(defaultDelay); | ||
} | ||
|
||
public void markDelay(T key) { | ||
this.markDelay(key, this.delaySettings.get()); | ||
/** | ||
* Creates a DefaultDelay with a custom maximum cache size. | ||
* | ||
* @param defaultDelay the default duration used for delays | ||
* @param maximumSize maximum number of entries in the cache | ||
* @param <T> the key type | ||
* @return DefaultDelay instance | ||
*/ | ||
public static <T> DefaultDelay<T> withDefault(Duration defaultDelay, long maximumSize) { | ||
return new GuavaDefaultDelay<>(defaultDelay, maximumSize); | ||
} | ||
|
||
public void unmarkDelay(T key) { | ||
this.delays.invalidate(key); | ||
/** | ||
* Creates an ExplicitDelay with the default cache size. | ||
* | ||
* @param <T> the key type | ||
* @return ExplicitDelay instance | ||
*/ | ||
public static <T> ExplicitDelay<T> explicit() { | ||
return new GuavaExplicitDelay<>(); | ||
} | ||
|
||
public boolean hasDelay(T key) { | ||
Instant delayExpireMoment = this.getDelayExpireMoment(key); | ||
|
||
return Instant.now().isBefore(delayExpireMoment); | ||
/** | ||
* Creates an ExplicitDelay with a custom maximum cache size. | ||
* | ||
* @param maximumSize maximum number of entries in the cache | ||
* @param <T> the key type | ||
* @return ExplicitDelay instance | ||
*/ | ||
public static <T> ExplicitDelay<T> explicit(long maximumSize) { | ||
return new GuavaExplicitDelay<>(maximumSize); | ||
} | ||
|
||
public Duration getDurationToExpire(T key) { | ||
return Duration.between(Instant.now(), this.getDelayExpireMoment(key)); | ||
} | ||
|
||
private Instant getDelayExpireMoment(T key) { | ||
return this.delays.asMap().getOrDefault(key, Instant.MIN); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
eternalcore-api/src/main/java/com/eternalcode/core/delay/ExplicitDelay.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,62 @@ | ||
package com.eternalcode.core.delay; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
/** | ||
* Represents a delay mechanism where each key must be marked with an explicit duration. | ||
* | ||
* @param <T> the type of key used to identify the delay | ||
*/ | ||
public interface ExplicitDelay<T> { | ||
|
||
/** | ||
* Marks a delay for the given key with the specified duration. | ||
* | ||
* @param key the key to mark | ||
* @param duration the duration of the delay | ||
*/ | ||
void markDelay(T key, Duration duration); | ||
|
||
/** | ||
* Removes any existing delay for the given key. | ||
* | ||
* @param key the key to unmark | ||
*/ | ||
void unmarkDelay(T key); | ||
|
||
/** | ||
* Checks if the given key currently has an active delay. | ||
* | ||
* @param key the key to check | ||
* @return true if the key has a delay, false otherwise | ||
*/ | ||
boolean hasDelay(T key); | ||
|
||
/** | ||
* Returns the remaining duration of the delay for the given key. | ||
* Returns {@code Duration.ZERO} if no active delay exists. | ||
* | ||
* @param key the key to check | ||
* @return remaining duration, or {@code Duration.ZERO} if none | ||
*/ | ||
Duration getRemaining(T key); | ||
|
||
/** | ||
* Returns the expiration time of the delay for the given key. | ||
* Returns {@code null} if no active delay exists. | ||
* | ||
* @param key the key to check | ||
* @return expiration instant, or {@code null} if none | ||
*/ | ||
Instant getExpireAt(T key); | ||
|
||
/** | ||
* Extends the delay for the given key by the specified extra duration. | ||
* If no active delay exists, a new one is created starting now. | ||
* | ||
* @param key the key to extend | ||
* @param extra the duration to add | ||
*/ | ||
void extendDelay(T key, Duration extra); | ||
} |
42 changes: 42 additions & 0 deletions
42
eternalcore-api/src/main/java/com/eternalcode/core/delay/GuavaDefaultDelay.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,42 @@ | ||
package com.eternalcode.core.delay; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* DefaultDelay implementation backed by {@link GuavaDelay} using Guava cache. | ||
* <p> | ||
* Each key marked without explicit duration uses a predefined default delay. | ||
* | ||
* @param <T> the type of key used to identify the delay | ||
*/ | ||
final class GuavaDefaultDelay<T> extends GuavaDelay<T> implements DefaultDelay<T> { | ||
|
||
private final Duration defaultDelay; | ||
|
||
/** | ||
* Creates a new DefaultDelay with the specified default duration | ||
* and the default maximum cache size. | ||
* | ||
* @param defaultDelay the default delay duration, must be positive | ||
*/ | ||
GuavaDefaultDelay(Duration defaultDelay) { | ||
this(defaultDelay, DEFAULT_MAXIMUM_SIZE); | ||
} | ||
|
||
/** | ||
* Creates a new DefaultDelay with the specified default duration | ||
* and a custom maximum cache size. | ||
* | ||
* @param defaultDelay the default delay duration, must be positive | ||
* @param maximumSize maximum number of entries allowed in the cache | ||
*/ | ||
GuavaDefaultDelay(Duration defaultDelay, long maximumSize) { | ||
super(defaultDelay, maximumSize); | ||
this.defaultDelay = defaultDelay; | ||
} | ||
imDMK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public void markDelay(T key) { | ||
putDelay(key, this.defaultDelay); | ||
} | ||
} |
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.