Reuse serialized server events across SSE connections
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import { observationReader } from '$lib/server/fleet/status.js';
|
||||
import { listServers } from '$lib/server/fleet/servers.js';
|
||||
import { statusEntry } from '$lib/server/pipeline/worker.js';
|
||||
import { subscribe } from '$lib/server/pipeline/bus.js';
|
||||
import { subscribe, type PipelineEvent } from '$lib/server/pipeline/bus.js';
|
||||
import { listTelemetry } from '$lib/server/fleet/telemetry.js';
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const HEARTBEAT_MS = 20_000;
|
||||
// Bus subscribers receive the same event object. Reuse its complete frame
|
||||
// across connections without retaining events after delivery.
|
||||
const frames = new WeakMap<PipelineEvent, Uint8Array>();
|
||||
|
||||
export async function GET() {
|
||||
let heartbeat: NodeJS.Timeout | null = null;
|
||||
@@ -15,7 +18,12 @@ export async function GET() {
|
||||
start(controller) {
|
||||
unsubscribe = subscribe((event) => {
|
||||
if (event.type === 'server-status' || event.type === 'server-telemetry') {
|
||||
controller.enqueue(encoder.encode(`event: ${event.type}\ndata: ${JSON.stringify({ ...event, serverNow: Date.now() })}\n\n`));
|
||||
let frame = frames.get(event);
|
||||
if (!frame) {
|
||||
frame = encoder.encode(`event: ${event.type}\ndata: ${JSON.stringify({ ...event, serverNow: Date.now() })}\n\n`);
|
||||
frames.set(event, frame);
|
||||
}
|
||||
controller.enqueue(frame);
|
||||
}
|
||||
});
|
||||
const observation = observationReader();
|
||||
|
||||
Reference in New Issue
Block a user