Skip to content

Commit 16cfa34

Browse files
perf(server): serve connect-time discovery from cache instead of probing
The server-config snapshot probed editors, the file manager and remote open targets on every connect, one after another, each behind a five second timeout. Serve all three stale-while-revalidate (refreshes bound to the service scope, capped at 30 seconds) and run them side by side, so a slow probe no longer delays the snapshot a client waits on to connect. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent fed41fa commit 16cfa34

7 files changed

Lines changed: 345 additions & 113 deletions

File tree

‎apps/server/src/environment/RemoteOpenTargets.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import * as Effect from "effect/Effect";
1717
import * as Layer from "effect/Layer";
1818
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
1919

20+
import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";
21+
2022
const SSH_PORT = 22;
23+
const TARGETS_CACHE_TTL = "60 seconds";
2124

2225
export class RemoteOpenTargets extends Context.Service<
2326
RemoteOpenTargets,
@@ -31,7 +34,7 @@ export const make = Effect.gen(function* () {
3134
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3235
const net = yield* NetService.NetService;
3336

34-
const resolveTargets = Effect.gen(function* () {
37+
const discoverTargets = Effect.gen(function* () {
3538
// No local sshd means no name can work; advertise nothing so clients
3639
// render a clear "no SSH route" state instead of links that hang.
3740
// Check both loopback families: sshd can be bound IPv6-only.
@@ -67,6 +70,10 @@ export const make = Effect.gen(function* () {
6770
return targets;
6871
});
6972

73+
// Advertised on every client connect; probing sshd and tailscaled is slow
74+
// on a loaded host, so serve the last result while it refreshes.
75+
const resolveTargets = yield* makeStaleWhileRevalidate(discoverTargets, TARGETS_CACHE_TTL);
76+
7077
return RemoteOpenTargets.of({ resolveTargets: () => resolveTargets });
7178
});
7279

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as Deferred from "effect/Deferred";
3+
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
5+
import * as Fiber from "effect/Fiber";
6+
import * as Ref from "effect/Ref";
7+
import * as Scope from "effect/Scope";
8+
import * as TestClock from "effect/testing/TestClock";
9+
10+
import { makeStaleWhileRevalidate } from "./staleWhileRevalidate.ts";
11+
12+
it.effect("computes inline once, then answers from memory until the ttl lapses", () =>
13+
Effect.gen(function* () {
14+
const calls = yield* Ref.make(0);
15+
const read = yield* makeStaleWhileRevalidate(
16+
Ref.updateAndGet(calls, (count) => count + 1),
17+
"60 seconds",
18+
);
19+
20+
assert.equal(yield* read, 1);
21+
yield* TestClock.adjust("59 seconds");
22+
assert.equal(yield* read, 1);
23+
assert.equal(yield* Ref.get(calls), 1);
24+
}).pipe(Effect.scoped),
25+
);
26+
27+
it.effect("serves the stale value while one background refresh replaces it", () =>
28+
Effect.gen(function* () {
29+
const calls = yield* Ref.make(0);
30+
const release = yield* Deferred.make<void>();
31+
const read = yield* makeStaleWhileRevalidate(
32+
Effect.gen(function* () {
33+
const call = yield* Ref.updateAndGet(calls, (count) => count + 1);
34+
if (call > 1) yield* Deferred.await(release);
35+
return call;
36+
}),
37+
"60 seconds",
38+
);
39+
40+
assert.equal(yield* read, 1);
41+
yield* TestClock.adjust("61 seconds");
42+
// The refresh is blocked, yet callers are answered immediately and only
43+
// one refresh starts however many callers arrive.
44+
assert.equal(yield* read, 1);
45+
assert.equal(yield* read, 1);
46+
yield* TestClock.adjust("1 second");
47+
assert.equal(yield* Ref.get(calls), 2);
48+
49+
yield* Deferred.succeed(release, undefined);
50+
yield* TestClock.adjust("1 second");
51+
assert.equal(yield* read, 2);
52+
}),
53+
);
54+
55+
it.effect("does not cache an interrupted first computation", () =>
56+
Effect.gen(function* () {
57+
const calls = yield* Ref.make(0);
58+
const read = yield* makeStaleWhileRevalidate(
59+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
60+
Effect.flatMap((call) => (call === 1 ? Effect.never : Effect.succeed(call))),
61+
),
62+
"60 seconds",
63+
);
64+
65+
const first = yield* read.pipe(Effect.forkChild);
66+
yield* Effect.yieldNow;
67+
yield* Fiber.interrupt(first);
68+
69+
assert.equal(yield* read, 2);
70+
}),
71+
);
72+
73+
it.effect("abandons a hung refresh so a later call can start another", () =>
74+
Effect.gen(function* () {
75+
const calls = yield* Ref.make(0);
76+
const read = yield* makeStaleWhileRevalidate(
77+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
78+
Effect.flatMap((call) => (call === 2 ? Effect.never : Effect.succeed(call))),
79+
),
80+
"60 seconds",
81+
);
82+
83+
assert.equal(yield* read, 1);
84+
yield* TestClock.adjust("61 seconds");
85+
assert.equal(yield* read, 1);
86+
yield* TestClock.adjust("1 second");
87+
assert.equal(yield* Ref.get(calls), 2);
88+
89+
// The hung refresh times out, releasing the claim for the next caller.
90+
yield* TestClock.adjust("30 seconds");
91+
assert.equal(yield* read, 1);
92+
yield* TestClock.adjust("1 second");
93+
assert.equal(yield* Ref.get(calls), 3);
94+
assert.equal(yield* read, 3);
95+
}).pipe(Effect.scoped),
96+
);
97+
98+
it.effect("interrupts a background refresh when the owning scope closes", () =>
99+
Effect.gen(function* () {
100+
const interrupted = yield* Deferred.make<void>();
101+
const scope = yield* Scope.make();
102+
const calls = yield* Ref.make(0);
103+
const read = yield* makeStaleWhileRevalidate(
104+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
105+
Effect.flatMap((call) =>
106+
call === 1
107+
? Effect.succeed(call)
108+
: Effect.never.pipe(Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined))),
109+
),
110+
),
111+
"60 seconds",
112+
).pipe(Scope.provide(scope));
113+
114+
yield* read;
115+
yield* TestClock.adjust("61 seconds");
116+
yield* read;
117+
yield* TestClock.adjust("1 second");
118+
yield* Scope.close(scope, Exit.void);
119+
yield* Deferred.await(interrupted);
120+
}),
121+
);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as Clock from "effect/Clock";
2+
import * as Duration from "effect/Duration";
3+
import * as Effect from "effect/Effect";
4+
import * as Option from "effect/Option";
5+
import * as Ref from "effect/Ref";
6+
7+
const REFRESH_TIMEOUT = Duration.seconds(30);
8+
9+
interface Entry<A> {
10+
readonly value: Option.Option<A>;
11+
readonly expiresAtNanos: bigint;
12+
readonly refreshing: boolean;
13+
}
14+
15+
/**
16+
* Memoizes a discovery that is cheap when warm and slow on a loaded host, for
17+
* callers that sit on the client connect path. The first call computes inline.
18+
* After that every call returns the last good value immediately; once the
19+
* value is older than `ttl`, one detached refresh replaces it in the background.
20+
*
21+
* Only successful results are stored, so a caller interrupted mid-scan (a
22+
* client disconnecting, a timeout around the call) leaves the cache untouched
23+
* instead of replaying the interrupt to later callers. Expiry uses the
24+
* monotonic clock so a wall-clock adjustment cannot keep a stale entry alive.
25+
*
26+
* Refreshes run in the scope that built the cache, so they are interrupted
27+
* (and any scoped probe process cleaned up) when the server shuts down, and
28+
* each is capped at `REFRESH_TIMEOUT` so a probe hung on an unresponsive mount
29+
* cannot leave the entry stale forever.
30+
*/
31+
export const makeStaleWhileRevalidate = <A>(discover: Effect.Effect<A>, ttl: Duration.Input) => {
32+
const ttlNanos = Duration.toNanosUnsafe(Duration.fromInputUnsafe(ttl));
33+
return Effect.gen(function* () {
34+
const scope = yield* Effect.scope;
35+
const state = yield* Ref.make<Entry<A>>({
36+
value: Option.none(),
37+
expiresAtNanos: 0n,
38+
refreshing: false,
39+
});
40+
const compute = Effect.gen(function* () {
41+
const value = yield* discover;
42+
const now = yield* Clock.monotonicTimeNanos;
43+
yield* Ref.set(state, {
44+
value: Option.some(value),
45+
expiresAtNanos: now + ttlNanos,
46+
refreshing: false,
47+
});
48+
return value;
49+
});
50+
const refresh = compute.pipe(
51+
Effect.timeoutOption(REFRESH_TIMEOUT),
52+
Effect.ignoreCause({ log: true }),
53+
Effect.ensuring(Ref.update(state, (entry) => ({ ...entry, refreshing: false }))),
54+
);
55+
return Effect.gen(function* () {
56+
const now = yield* Clock.monotonicTimeNanos;
57+
const entry = yield* Ref.get(state);
58+
if (Option.isNone(entry.value)) return yield* compute;
59+
if (entry.expiresAtNanos <= now) {
60+
// Claiming and forking are one uninterruptible step: a caller
61+
// interrupted between them would leave `refreshing` set with no
62+
// refresh running, and every later call would serve stale data.
63+
yield* Effect.uninterruptible(
64+
Ref.modify(state, (current) =>
65+
current.refreshing
66+
? ([false, current] as const)
67+
: ([true, { ...current, refreshing: true }] as const),
68+
).pipe(
69+
Effect.flatMap((claimed) => (claimed ? Effect.forkIn(refresh, scope) : Effect.void)),
70+
),
71+
);
72+
}
73+
return entry.value.value;
74+
});
75+
});
76+
};

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,65 +1126,71 @@ it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launc
11261126
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
11271127
);
11281128

1129-
it.effect("memoizes editor discovery and refreshes after the cache window", () => {
1130-
let statCalls = 0;
1131-
const fileInfo = { type: "File" } as FileSystem.File.Info;
1132-
const launcherLayer = ExternalLauncher.layer.pipe(
1133-
Layer.provide(
1134-
Layer.mergeAll(
1135-
FileSystem.layerNoop({
1136-
stat: () =>
1137-
Effect.sync(() => {
1138-
statCalls += 1;
1139-
return fileInfo;
1140-
}),
1141-
}),
1142-
Path.layer,
1143-
Layer.succeed(
1144-
ChildProcessSpawner.ChildProcessSpawner,
1145-
ChildProcessSpawner.make(() => Effect.sync(() => makeMockDetachedHandle())),
1129+
it.effect(
1130+
"memoizes editor discovery and refreshes in the background after the cache window",
1131+
() => {
1132+
let statCalls = 0;
1133+
const fileInfo = { type: "File" } as FileSystem.File.Info;
1134+
const launcherLayer = ExternalLauncher.layer.pipe(
1135+
Layer.provide(
1136+
Layer.mergeAll(
1137+
FileSystem.layerNoop({
1138+
stat: () =>
1139+
Effect.sync(() => {
1140+
statCalls += 1;
1141+
return fileInfo;
1142+
}),
1143+
}),
1144+
Path.layer,
1145+
Layer.succeed(
1146+
ChildProcessSpawner.ChildProcessSpawner,
1147+
ChildProcessSpawner.make(() => Effect.sync(() => makeMockDetachedHandle())),
1148+
),
11461149
),
11471150
),
1148-
),
1149-
);
1151+
);
11501152

1151-
return Effect.gen(function* () {
1152-
const launcher = yield* ExternalLauncher.ExternalLauncher;
1153+
return Effect.gen(function* () {
1154+
const launcher = yield* ExternalLauncher.ExternalLauncher;
11531155

1154-
const first = yield* launcher.resolveAvailableEditors();
1155-
assert.equal(first.includes("vscode"), true);
1156-
const statCallsAfterFirstScan = statCalls;
1157-
assert.isAbove(statCallsAfterFirstScan, 0);
1158-
1159-
// Past the shared command-resolution cache TTL (30s) but within the
1160-
// discovery cache window: the memoized set is reused without any scan.
1161-
yield* TestClock.adjust("31 seconds");
1162-
const second = yield* launcher.resolveAvailableEditors();
1163-
assert.deepEqual([...second], [...first]);
1164-
assert.equal(statCalls, statCallsAfterFirstScan);
1165-
1166-
// Past the discovery cache window the next call rescans.
1167-
yield* TestClock.adjust("30 seconds");
1168-
yield* launcher.resolveAvailableEditors();
1169-
assert.isAbove(statCalls, statCallsAfterFirstScan);
1170-
}).pipe(
1171-
Effect.provide(
1172-
Layer.mergeAll(
1173-
launcherLayer,
1174-
Layer.succeed(HostProcessPlatform, "win32"),
1175-
ConfigProvider.layer(
1176-
ConfigProvider.fromEnv({
1177-
env: {
1178-
PATH: "C:\\t3-editor-discovery-cache-test",
1179-
PATHEXT: ".COM;.EXE;.BAT;.CMD",
1180-
},
1181-
}),
1156+
const first = yield* launcher.resolveAvailableEditors();
1157+
assert.equal(first.includes("vscode"), true);
1158+
const statCallsAfterFirstScan = statCalls;
1159+
assert.isAbove(statCallsAfterFirstScan, 0);
1160+
1161+
// Past the shared command-resolution cache TTL (30s) but within the
1162+
// discovery cache window: the memoized set is reused without any scan.
1163+
yield* TestClock.adjust("31 seconds");
1164+
const second = yield* launcher.resolveAvailableEditors();
1165+
assert.deepEqual([...second], [...first]);
1166+
assert.equal(statCalls, statCallsAfterFirstScan);
1167+
1168+
// Past the discovery cache window the next call still answers from the
1169+
// memoized set and rescans in the background.
1170+
yield* TestClock.adjust("30 seconds");
1171+
const third = yield* launcher.resolveAvailableEditors();
1172+
assert.deepEqual([...third], [...first]);
1173+
yield* Effect.yieldNow;
1174+
assert.isAbove(statCalls, statCallsAfterFirstScan);
1175+
}).pipe(
1176+
Effect.provide(
1177+
Layer.mergeAll(
1178+
launcherLayer,
1179+
Layer.succeed(HostProcessPlatform, "win32"),
1180+
ConfigProvider.layer(
1181+
ConfigProvider.fromEnv({
1182+
env: {
1183+
PATH: "C:\\t3-editor-discovery-cache-test",
1184+
PATHEXT: ".COM;.EXE;.BAT;.CMD",
1185+
},
1186+
}),
1187+
),
1188+
TestClock.layer(),
11821189
),
1183-
TestClock.layer(),
11841190
),
1185-
),
1186-
);
1187-
});
1191+
);
1192+
},
1193+
);
11881194

11891195
// A client that disconnects mid-scan interrupts the shared discovery effect on
11901196
// the connection fiber. The cache must not retain that interrupt: doing so

0 commit comments

Comments
 (0)