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 c208214
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 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,30 @@ 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) {
// This allows the references in the context (eg req) to become available
// for GC earlier - as soon as close() is called. W/o it the references
// would remain is scope until the setTimeout expires.
clearTimeout(heartbeatTimeout);
heartbeatTimeout = null;
}
}

onFinished(req, close);
Expand Down

0 comments on commit c208214

Please sign in to comment.