Skip to content

Commit

Permalink
logs: Eagerly clear the heartbeat timeout when the request finishes
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
thgreasi committed Jul 4, 2024
1 parent 81f7293 commit 8befed8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/features/device-logs/lib/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,27 @@ async function handleStreamingRead(
}
});

let heartbeatTimeout: ReturnType<typeof setTimeout> | null = null;

function heartbeat() {
if (state !== StreamState.Closed) {
// In order to keep the connection alive, output new lines every now and then
write('\n');
setTimeout(heartbeat, LOGS_HEARTBEAT_INTERVAL);
heartbeatTimeout = setTimeout(heartbeat, LOGS_HEARTBEAT_INTERVAL);
}
}

setTimeout(heartbeat, LOGS_HEARTBEAT_INTERVAL);
heartbeatTimeout = setTimeout(heartbeat, LOGS_HEARTBEAT_INTERVAL);

function close() {
if (state !== StreamState.Closed) {
state = StreamState.Closed;
getBackend().unsubscribe(ctx, onLog);
}
if (heartbeatTimeout != null) {
clearTimeout(heartbeatTimeout);
heartbeatTimeout = null;
}
}

onFinished(req, close);
Expand Down

0 comments on commit 8befed8

Please sign in to comment.