-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeartbeatTracker.java
More file actions
40 lines (32 loc) · 1.19 KB
/
HeartbeatTracker.java
File metadata and controls
40 lines (32 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package dev.arcp.runtime.heartbeat;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.atomic.AtomicReference;
/**
* Tracks last inbound activity per session. The dead-link decision is "two consecutive missed
* intervals" — at every scheduler tick the runtime asks {@link #shouldClose(Duration)} and acts on
* a {@code true} reply.
*/
public final class HeartbeatTracker {
private final Clock clock;
private final AtomicReference<Instant> lastInbound;
public HeartbeatTracker(Clock clock) {
this.clock = clock;
this.lastInbound = new AtomicReference<>(clock.instant());
}
public void onInbound() {
lastInbound.set(clock.instant());
}
public Duration sinceLastInbound() {
return Duration.between(lastInbound.get(), clock.instant());
}
/** Per §6.4: two consecutive missed intervals MAY close the transport. */
public boolean shouldClose(Duration interval) {
return sinceLastInbound().compareTo(interval.multipliedBy(2)) > 0;
}
/** Per §6.4: one elapsed interval triggers an outbound ping. */
public boolean shouldPing(Duration interval) {
return sinceLastInbound().compareTo(interval) >= 0;
}
}