-
Notifications
You must be signed in to change notification settings - Fork 0
Factor out different DelayManager implementations #115
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
Changes from all commits
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,65 @@ | ||
/* | ||
* Copyright 2025 Andrew Aylett | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.aylett.arc.internal; | ||
|
||
import org.checkerframework.checker.lock.qual.MayReleaseLocks; | ||
|
||
import java.time.Duration; | ||
import java.time.InstantSource; | ||
import java.util.concurrent.DelayQueue; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
public final class ExpireAndRefreshDelayManager extends DelayManager { | ||
private final DelayQueue<TimeDelayedElement> queue; | ||
private final DelayQueue<TimeDelayedElement> refreshQueue; | ||
private final Duration expiry; | ||
private final Duration refresh; | ||
|
||
public ExpireAndRefreshDelayManager(Duration expiry, Duration refresh, InstantSource timeSource) { | ||
super(timeSource); | ||
checkArgument(expiry.compareTo(refresh) >= 0, "Expiry must be greater than refresh"); | ||
Check warning on line 35 in lib/src/main/java/eu/aylett/arc/internal/ExpireAndRefreshDelayManager.java
|
||
checkArgument(expiry.isPositive(), "Expiry must be positive"); | ||
Check warning on line 36 in lib/src/main/java/eu/aylett/arc/internal/ExpireAndRefreshDelayManager.java
|
||
checkArgument(refresh.isPositive(), "Refresh must be positive"); | ||
Check warning on line 37 in lib/src/main/java/eu/aylett/arc/internal/ExpireAndRefreshDelayManager.java
|
||
this.queue = new DelayQueue<>(); | ||
this.refreshQueue = new DelayQueue<>(); | ||
this.expiry = expiry; | ||
this.refresh = refresh; | ||
} | ||
|
||
@Override | ||
public DelayedElement add(Element<?, ?> element) { | ||
var epochMilli = timeSource.instant().toEpochMilli(); | ||
var delayedElement = new TimeDelayedElement(element, this::getDelay, epochMilli + expiry.toMillis()); | ||
queue.add(delayedElement); | ||
refreshQueue.add(new TimeDelayedElement(element, this::getDelay, epochMilli + refresh.toMillis())); | ||
return delayedElement; | ||
} | ||
|
||
@MayReleaseLocks | ||
@Override | ||
public void poll() { | ||
TimeDelayedElement element; | ||
while ((element = refreshQueue.poll()) != null) { | ||
element.refresh(); | ||
} | ||
while ((element = queue.poll()) != null) { | ||
element.expireFromDelay(); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2025 Andrew Aylett | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.aylett.arc.internal; | ||
|
||
import org.checkerframework.checker.lock.qual.MayReleaseLocks; | ||
|
||
import java.time.Duration; | ||
import java.time.InstantSource; | ||
import java.util.concurrent.DelayQueue; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
public final class ExpiringDelayManager extends DelayManager { | ||
private final DelayQueue<TimeDelayedElement> queue; | ||
private final Duration expiry; | ||
|
||
public ExpiringDelayManager(Duration expiry, InstantSource timeSource) { | ||
super(timeSource); | ||
checkArgument(expiry.isPositive(), "Expiry must be positive"); | ||
Check warning on line 33 in lib/src/main/java/eu/aylett/arc/internal/ExpiringDelayManager.java
|
||
this.queue = new DelayQueue<>(); | ||
this.expiry = expiry; | ||
} | ||
|
||
@Override | ||
public DelayedElement add(Element<?, ?> element) { | ||
var epochMilli = timeSource.instant().toEpochMilli(); | ||
var delayedElement = new TimeDelayedElement(element, this::getDelay, epochMilli + expiry.toMillis()); | ||
queue.add(delayedElement); | ||
return delayedElement; | ||
Check warning on line 43 in lib/src/main/java/eu/aylett/arc/internal/ExpiringDelayManager.java
|
||
} | ||
|
||
@MayReleaseLocks | ||
@Override | ||
public void poll() { | ||
TimeDelayedElement element; | ||
while ((element = queue.poll()) != null) { | ||
Check warning on line 50 in lib/src/main/java/eu/aylett/arc/internal/ExpiringDelayManager.java
|
||
element.expireFromDelay(); | ||
Check warning on line 51 in lib/src/main/java/eu/aylett/arc/internal/ExpiringDelayManager.java
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2025 Andrew Aylett | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.aylett.arc.internal; | ||
|
||
import org.checkerframework.checker.lock.qual.MayReleaseLocks; | ||
|
||
import java.time.InstantSource; | ||
|
||
public final class NoOpDelayManager extends DelayManager { | ||
|
||
public NoOpDelayManager(InstantSource timeSource) { | ||
super(timeSource); | ||
} | ||
|
||
@Override | ||
public DelayedElement add(Element<?, ?> element) { | ||
return new NoOpDelayedElement(); | ||
} | ||
|
||
@MayReleaseLocks | ||
@Override | ||
public void poll() { | ||
} | ||
} |
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.
[nitpick] The condition verifying expiry and refresh is somewhat unclear; consider rewriting it to explicitly check that if a refresh duration is provided, then expiry must be non-null, for example by using a clear if statement with an explicit error message.
Copilot uses AI. Check for mistakes.