- private void scheduleTimer(final FiniteDuration delay) {
- LOG.debug("{}: scheduling timeout in {}", context.persistenceId(), delay);
- context.executeInActor(this::runTimer, delay);
+ @GuardedBy("lock")
+ private void scheduleTimer(final long delay) {
+ if (haveTimer) {
+ LOG.debug("{}: timer already scheduled", context.persistenceId());
+ return;
+ }
+ if (queue.hasSuccessor()) {
+ LOG.debug("{}: connection has successor, not scheduling timer", context.persistenceId());
+ return;
+ }
+
+ // If the delay is negative, we need to schedule an action immediately. While the caller could have checked
+ // for that condition and take appropriate action, but this is more convenient and less error-prone.
+ final long normalized = delay <= 0 ? 0 : Math.min(delay, BACKEND_ALIVE_TIMEOUT_NANOS);
+
+ final FiniteDuration dur = FiniteDuration.fromNanos(normalized);
+ LOG.debug("{}: scheduling timeout in {}", context.persistenceId(), dur);
+ context.executeInActor(this::runTimer, dur);
+ haveTimer = true;