Skip to content
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

Bring back client expecting heartbeats #231

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions transport/sessionStateMachine/SessionConnected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ export class SessionConnected<
conn: ConnType;
listeners: SessionConnectedListeners;

heartbeatHandle?: ReturnType<typeof setInterval> | undefined;
heartbeatMisses = 0;
private activeHeartbeatHandle?: ReturnType<typeof setInterval> | undefined;
private activeHeartbeatMisses = 0;

private passiveHeartbeatHandle?: ReturnType<typeof setTimeout> | undefined;

get isActivelyHeartbeating() {
return this.heartbeatHandle !== undefined;
return this.activeHeartbeatHandle !== undefined;
}

updateBookkeeping(ack: number, seq: number) {
this.sendBuffer = this.sendBuffer.filter((unacked) => unacked.seq >= ack);
this.ack = seq + 1;
this.heartbeatMisses = 0;
this.activeHeartbeatMisses = 0;
}

send(msg: PartialTransportMessage): string {
Expand Down Expand Up @@ -80,8 +82,8 @@ export class SessionConnected<
}

startActiveHeartbeat() {
this.heartbeatHandle = setInterval(() => {
const misses = this.heartbeatMisses;
this.activeHeartbeatHandle = setInterval(() => {
const misses = this.activeHeartbeatMisses;
const missDuration = misses * this.options.heartbeatIntervalMs;
if (misses >= this.options.heartbeatsUntilDead) {
this.log?.info(
Expand All @@ -90,16 +92,36 @@ export class SessionConnected<
);
this.telemetry.span.addEvent('closing connection due to inactivity');
this.conn.close();
clearInterval(this.heartbeatHandle);
this.heartbeatHandle = undefined;
clearInterval(this.activeHeartbeatHandle);
this.activeHeartbeatHandle = undefined;
return;
}

this.sendHeartbeat();
this.heartbeatMisses++;
this.activeHeartbeatMisses++;
}, this.options.heartbeatIntervalMs);
}

waitForNextHeartbeat() {
const duration =
this.options.heartbeatsUntilDead * this.options.heartbeatIntervalMs;

if (this.passiveHeartbeatHandle) {
clearTimeout(this.passiveHeartbeatHandle);
this.passiveHeartbeatHandle = undefined;
}

this.passiveHeartbeatHandle = setTimeout(() => {
this.log?.info(
`closing connection to ${this.to} due to not receiving a heartbeat in the last ${duration}ms`,
this.loggingMetadata,
);
this.telemetry.span.addEvent('closing connection due to inactivity');
this.conn.close();
this.passiveHeartbeatHandle = undefined;
}, duration);
}

private sendHeartbeat() {
this.log?.debug('sending heartbeat', this.loggingMetadata);
this.send({
Expand Down Expand Up @@ -167,6 +189,7 @@ export class SessionConnected<
// heartbeat mode and should send a response to the ack
if (!this.isActivelyHeartbeating) {
this.sendHeartbeat();
this.waitForNextHeartbeat();
}
};

Expand All @@ -175,8 +198,10 @@ export class SessionConnected<
this.conn.removeDataListener(this.onMessageData);
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
clearInterval(this.heartbeatHandle);
this.heartbeatHandle = undefined;
clearInterval(this.activeHeartbeatHandle);
clearTimeout(this.passiveHeartbeatHandle);
this.activeHeartbeatHandle = undefined;
this.passiveHeartbeatHandle = undefined;
}

_handleClose(): void {
Expand Down
Loading