Skip to content

Commit

Permalink
[SDCISA-15833, swisspost#170] Reduce risk of 'onDone()' from being ca…
Browse files Browse the repository at this point in the history
…lled too many times.
  • Loading branch information
hiddenalpha committed May 7, 2024
1 parent a659474 commit 40af328
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.vertx.core.Vertx;
import org.slf4j.Logger;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

import static java.lang.System.currentTimeMillis;
Expand Down Expand Up @@ -47,10 +48,16 @@ private void onTrigger(Timer timer) {
return;
}
timer.begEpochMs = currentTimeMillis();
boolean oldValue = timer.isPending.getAndSet(true);
assert !oldValue : "Why is this already pending?";
timer.task.accept(timer::onTaskDone_);
}

private void onTaskDone(Timer timer) {
boolean oldVal = timer.isPending.getAndSet(false);
if (!oldVal) {
throw new IllegalStateException("MUST NOT be called multiple times!");
}
timer.endEpochMs = currentTimeMillis();
}

Expand All @@ -64,6 +71,7 @@ public class Timer {
private long id;
// When the last run has begun and end.
private long begEpochMs, endEpochMs;
private final AtomicBoolean isPending = new AtomicBoolean();

private Timer(Consumer<Runnable> task) {
this.task = task;
Expand Down

0 comments on commit 40af328

Please sign in to comment.