Skip to content
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions apps/server/src/socketHeartbeat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { assert, describe, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Scope from "effect/Scope";
import * as Socket from "effect/socket/Socket";
import * as TestClock from "effect/testing/TestClock";

import {
SOCKET_HEARTBEAT_FRAME,
SOCKET_HEARTBEAT_INTERVAL,
withSocketHeartbeat,
} from "./socketHeartbeat.ts";

describe("withSocketHeartbeat", () => {
it.effect("writes a heartbeat each interval until the writer's scope closes", () =>
Effect.gen(function* () {
const written: Array<string | Uint8Array | Socket.CloseEvent> = [];
const socket = withSocketHeartbeat(
Socket.make({
reader: Effect.never,
writer: Effect.succeed({
write: (chunk) => Effect.sync(() => void written.push(chunk)),
writeAll: (chunks) => Effect.sync(() => void written.push(...chunks)),
}),
}),
);
const scope = yield* Scope.make();
yield* Scope.provide(socket.writer, scope);

yield* TestClock.adjust(SOCKET_HEARTBEAT_INTERVAL);
yield* TestClock.adjust(SOCKET_HEARTBEAT_INTERVAL);
assert.deepStrictEqual(written, [SOCKET_HEARTBEAT_FRAME, SOCKET_HEARTBEAT_FRAME]);

yield* Scope.close(scope, Exit.void);
yield* TestClock.adjust(SOCKET_HEARTBEAT_INTERVAL);
assert.strictEqual(written.length, 2);
}),
);
});
27 changes: 27 additions & 0 deletions apps/server/src/socketHeartbeat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Socket from "effect/socket/Socket";

// Reverse proxies close a socket after 60 seconds without upstream bytes
// (nginx proxy_read_timeout, AWS ALB), and a suspended client stops pinging.
export const SOCKET_HEARTBEAT_INTERVAL = Duration.seconds(20);

// RPC clients count any decoded frame as liveness and ignore unsolicited pongs.
export const SOCKET_HEARTBEAT_FRAME = '{"_tag":"Pong"}';

/** Server side: writes a heartbeat frame on an interval for as long as the writer is open. */
export function withSocketHeartbeat(socket: Socket.Socket): Socket.Socket {
return Socket.make({
reader: socket.reader,
writer: Effect.tap(socket.writer, (writer) =>
writer
.write(SOCKET_HEARTBEAT_FRAME)
.pipe(
Effect.delay(SOCKET_HEARTBEAT_INTERVAL),
Effect.forever,
Effect.ignore,
Effect.forkScoped,
),
),
});
}
30 changes: 16 additions & 14 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ import * as StorageCleanup from "./storageCleanup.ts";
import * as TerminalManager from "./terminal/Manager.ts";
import { makePrepareTerminalLaunch } from "./workspace/restoreMissingWorktree.ts";
import { terminalOutputStream, withOutputWindow } from "./terminal/OutputProtocol.ts";
import { withSocketHeartbeat } from "./socketHeartbeat.ts";
import * as ServerBrowser from "./preview/ServerBrowser.ts";
import * as DeviceService from "./device/DeviceService.ts";
import { remoteSshDeviceHosts } from "./device/localSshDeviceHost.ts";
Expand Down Expand Up @@ -3826,20 +3827,21 @@ export const layer = Layer.unwrap(
),
),
);
const framedRpcWebSocketHttpEffect = url.value.searchParams.has(
SOCKET_FRAME_PARTS_QUERY_PARAM,
)
? rpcWebSocketHttpEffect.pipe(
Effect.provideService(
HttpServerRequest.HttpServerRequest,
// The RPC server upgrades the request itself, so its socket is
// wrapped through the request it reads.
Object.create(request, {
upgrade: { value: Effect.map(request.upgrade, splitLargeSocketFrames) },
}),
),
)
: rpcWebSocketHttpEffect;
const splitFrames = url.value.searchParams.has(SOCKET_FRAME_PARTS_QUERY_PARAM);
const framedRpcWebSocketHttpEffect = rpcWebSocketHttpEffect.pipe(
Effect.provideService(
HttpServerRequest.HttpServerRequest,
// The RPC server upgrades the request itself, so its socket is
// wrapped through the request it reads.
Object.create(request, {
upgrade: {
value: Effect.map(request.upgrade, (socket) =>
withSocketHeartbeat(splitFrames ? splitLargeSocketFrames(socket) : socket),
),
},
}),
),
);
return yield* Effect.acquireUseRelease(
sessions.markConnected(session.sessionId),
() =>
Expand Down
Loading