-
-
Notifications
You must be signed in to change notification settings - Fork 0
GH-76 Add delay #76
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
GH-76 Add delay #76
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.eternalcode.commons.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; | ||
|
|
||
| public class Delay<T> { | ||
|
|
||
| private final Cache<T, Instant> cache; | ||
| private final Supplier<Duration> defaultDelay; | ||
|
|
||
| private Delay(Supplier<Duration> defaultDelay) { | ||
Jakubk15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (defaultDelay == null) { | ||
| throw new IllegalArgumentException("defaultDelay cannot be null"); | ||
| } | ||
|
|
||
| this.defaultDelay = defaultDelay; | ||
| this.cache = Caffeine.newBuilder() | ||
| .expireAfter(new InstantExpiry<T>()) | ||
| .build(); | ||
|
Comment on lines
+23
to
+25
Contributor
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. The Caffeine cache is initialized without a maximum size, which can lead to memory exhaustion if a large number of unique keys are added. This is especially risky if the keys are derived from untrusted user input (e.g., unique user IDs or IP addresses). Adding a limit prevents the cache from growing indefinitely. this.cache = Caffeine.newBuilder()
.expireAfter(new InstantExpiry<T>())
.maximumSize(1000) // TODO: Adjust this value based on expected usage
.build();
Member
Author
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. I don't know if this will affect our use-cases |
||
| } | ||
|
|
||
| public static <T> Delay<T> withDefault(Supplier<Duration> defaultDelay) { | ||
| return new Delay<>(defaultDelay); | ||
| } | ||
|
|
||
| public void markDelay(T key, Duration delay) { | ||
| if (delay.isZero() || delay.isNegative()) { | ||
| this.cache.invalidate(key); | ||
| } | ||
|
|
||
| this.cache.put(key, Instant.now().plus(delay)); | ||
Jakubk15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void markDelay(T key) { | ||
| this.markDelay(key, this.defaultDelay.get()); | ||
| } | ||
|
|
||
| public void unmarkDelay(T key) { | ||
| this.cache.invalidate(key); | ||
| } | ||
|
|
||
| public boolean hasDelay(T key) { | ||
| Instant delayExpireMoment = this.getExpireAt(key); | ||
| return Instant.now().isBefore(delayExpireMoment); | ||
| } | ||
|
|
||
| public Duration getRemaining(T key) { | ||
| return Duration.between(Instant.now(), this.getExpireAt(key)); | ||
| } | ||
Jakubk15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private Instant getExpireAt(T key) { | ||
| return this.cache.asMap().getOrDefault(key, Instant.MIN); | ||
| } | ||
Jakubk15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.eternalcode.commons.delay; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Expiry; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
|
|
||
| public class InstantExpiry<T> implements Expiry<T, Instant> { | ||
Jakubk15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private static long timeToExpire(Instant expireTime) { | ||
| Duration toExpire = Duration.between(Instant.now(), expireTime); | ||
| if (toExpire.isNegative()) { | ||
| return 0; | ||
| } | ||
|
|
||
| long nanos = toExpire.toNanos(); | ||
Jakubk15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (nanos == 0) { | ||
| return 1; | ||
| } | ||
|
|
||
| return nanos; | ||
| } | ||
|
|
||
| @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; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.