Skip to content

Commit 7b9e1f3

Browse files
perf(server): serve connect-time discovery from cache instead of probing
The server-config snapshot probed editors, the file manager, remote open targets and direct endpoints on every connect, one after another, each behind a five second timeout. Serve editors, the file manager and remote open targets from one stale-while-revalidate helper and run all four side by side, so a slow probe no longer delays the snapshot a client waits on to connect. The helper keeps the shared first scan from #13917: scans run in the service scope, callers' timeouts and disconnects cannot cancel them, and later callers join the running scan. Background refreshes are capped at 30 seconds. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 37de6cb commit 7b9e1f3

7 files changed

Lines changed: 393 additions & 146 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: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 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("shares one first scan that an interrupted caller does not cancel", () =>
56+
Effect.gen(function* () {
57+
const calls = yield* Ref.make(0);
58+
const release = yield* Deferred.make<void>();
59+
const read = yield* makeStaleWhileRevalidate(
60+
Ref.updateAndGet(calls, (count) => count + 1).pipe(Effect.tap(() => Deferred.await(release))),
61+
"60 seconds",
62+
);
63+
64+
// A connect that times out or disconnects mid-scan leaves the scan running.
65+
const interrupted = yield* read.pipe(Effect.forkChild);
66+
yield* Effect.yieldNow;
67+
yield* Fiber.interrupt(interrupted);
68+
69+
const next = yield* read.pipe(Effect.forkChild);
70+
yield* Effect.yieldNow;
71+
yield* Deferred.succeed(release, undefined);
72+
assert.equal(yield* Fiber.join(next), 1);
73+
assert.equal(yield* Ref.get(calls), 1);
74+
}).pipe(Effect.scoped),
75+
);
76+
77+
it.effect("starts over after a failed first scan", () =>
78+
Effect.gen(function* () {
79+
const calls = yield* Ref.make(0);
80+
const read = yield* makeStaleWhileRevalidate(
81+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
82+
Effect.flatMap((call) => (call === 1 ? Effect.die("probe crashed") : Effect.succeed(call))),
83+
),
84+
"60 seconds",
85+
);
86+
87+
assert.isTrue(Exit.isFailure(yield* Effect.exit(read)));
88+
assert.equal(yield* read, 2);
89+
}).pipe(Effect.scoped),
90+
);
91+
92+
it.effect("abandons a hung refresh so a later call can start another", () =>
93+
Effect.gen(function* () {
94+
const calls = yield* Ref.make(0);
95+
const read = yield* makeStaleWhileRevalidate(
96+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
97+
Effect.flatMap((call) => (call === 2 ? Effect.never : Effect.succeed(call))),
98+
),
99+
"60 seconds",
100+
);
101+
102+
assert.equal(yield* read, 1);
103+
yield* TestClock.adjust("61 seconds");
104+
assert.equal(yield* read, 1);
105+
yield* TestClock.adjust("1 second");
106+
assert.equal(yield* Ref.get(calls), 2);
107+
108+
// The hung refresh times out, releasing the claim for the next caller.
109+
yield* TestClock.adjust("30 seconds");
110+
assert.equal(yield* read, 1);
111+
yield* TestClock.adjust("1 second");
112+
assert.equal(yield* Ref.get(calls), 3);
113+
assert.equal(yield* read, 3);
114+
}).pipe(Effect.scoped),
115+
);
116+
117+
it.effect("interrupts a background refresh when the owning scope closes", () =>
118+
Effect.gen(function* () {
119+
const interrupted = yield* Deferred.make<void>();
120+
const scope = yield* Scope.make();
121+
const calls = yield* Ref.make(0);
122+
const read = yield* makeStaleWhileRevalidate(
123+
Ref.updateAndGet(calls, (count) => count + 1).pipe(
124+
Effect.flatMap((call) =>
125+
call === 1
126+
? Effect.succeed(call)
127+
: Effect.never.pipe(Effect.onInterrupt(() => Deferred.succeed(interrupted, undefined))),
128+
),
129+
),
130+
"60 seconds",
131+
).pipe(Scope.provide(scope));
132+
133+
yield* read;
134+
yield* TestClock.adjust("61 seconds");
135+
yield* read;
136+
yield* TestClock.adjust("1 second");
137+
yield* Scope.close(scope, Exit.void);
138+
yield* Deferred.await(interrupted);
139+
}),
140+
);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import * as Clock from "effect/Clock";
2+
import * as Deferred from "effect/Deferred";
3+
import * as Duration from "effect/Duration";
4+
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
6+
import * as Ref from "effect/Ref";
7+
8+
const REFRESH_TIMEOUT = Duration.seconds(30);
9+
10+
type Entry<A> =
11+
| { readonly _tag: "Empty" }
12+
| { readonly _tag: "Scanning"; readonly scan: Deferred.Deferred<A> }
13+
| {
14+
readonly _tag: "Ready";
15+
readonly value: A;
16+
readonly expiresAtNanos: bigint;
17+
readonly refreshing: boolean;
18+
};
19+
20+
/**
21+
* Memoizes a discovery that is cheap when warm and slow on a loaded host, for
22+
* callers that sit on the client connect path. Until a value exists, callers
23+
* wait on one shared scan. After that every call returns the last good value
24+
* immediately; once the value is older than `ttl`, one refresh replaces it in
25+
* the background.
26+
*
27+
* Scans run on their own fiber in the scope that built the cache, never on a
28+
* caller's. Callers time out and disconnect mid-connect, and neither may cancel
29+
* a scan other callers are waiting on, or throw away work a slow host needs
30+
* more than one connect to finish. Only successes are stored: a failed scan
31+
* leaves the previous state, so the next caller starts over. Expiry uses the
32+
* monotonic clock so a wall-clock adjustment cannot keep a stale entry alive.
33+
*
34+
* Closing the scope interrupts any scan (and cleans up a scoped probe process).
35+
* A refresh is capped at `REFRESH_TIMEOUT` so a probe hung on an unresponsive
36+
* mount cannot leave the entry stale forever; a first scan is not, because
37+
* callers that already timed out still pick up its result later.
38+
*/
39+
export const makeStaleWhileRevalidate = <A>(discover: Effect.Effect<A>, ttl: Duration.Input) => {
40+
const ttlNanos = Duration.toNanosUnsafe(Duration.fromInputUnsafe(ttl));
41+
return Effect.gen(function* () {
42+
const scope = yield* Effect.scope;
43+
const state = yield* Ref.make<Entry<A>>({ _tag: "Empty" });
44+
45+
const settle = (exit: Exit.Exit<A>) =>
46+
Effect.gen(function* () {
47+
const now = yield* Clock.monotonicTimeNanos;
48+
yield* Ref.update(state, (entry): Entry<A> =>
49+
Exit.isSuccess(exit)
50+
? {
51+
_tag: "Ready",
52+
value: exit.value,
53+
expiresAtNanos: now + ttlNanos,
54+
refreshing: false,
55+
}
56+
: entry._tag === "Ready"
57+
? { ...entry, refreshing: false }
58+
: { _tag: "Empty" },
59+
);
60+
});
61+
const fork = (scan: Effect.Effect<unknown>) =>
62+
scan.pipe(Effect.ignoreCause({ log: true }), Effect.interruptible, Effect.forkIn(scope));
63+
const firstScan = (scan: Deferred.Deferred<A>) =>
64+
fork(
65+
discover.pipe(
66+
Effect.onExit((exit) => settle(exit).pipe(Effect.andThen(Deferred.done(scan, exit)))),
67+
),
68+
);
69+
const refresh = fork(
70+
discover.pipe(Effect.onExit(settle), Effect.timeoutOption(REFRESH_TIMEOUT)),
71+
);
72+
73+
return Effect.gen(function* () {
74+
const now = yield* Clock.monotonicTimeNanos;
75+
// Claiming a scan and starting it are one uninterruptible step: a caller
76+
// interrupted between them would leave a claimed scan that never runs.
77+
const result = yield* Ref.modify(
78+
state,
79+
(entry): readonly [readonly [Effect.Effect<unknown>, Effect.Effect<A>], Entry<A>] => {
80+
switch (entry._tag) {
81+
case "Empty": {
82+
const scan = Deferred.makeUnsafe<A>();
83+
return [[firstScan(scan), Deferred.await(scan)], { _tag: "Scanning", scan }];
84+
}
85+
case "Scanning":
86+
return [[Effect.void, Deferred.await(entry.scan)], entry];
87+
case "Ready":
88+
return entry.refreshing || entry.expiresAtNanos > now
89+
? [[Effect.void, Effect.succeed(entry.value)], entry]
90+
: [[refresh, Effect.succeed(entry.value)], { ...entry, refreshing: true }];
91+
}
92+
},
93+
).pipe(
94+
Effect.flatMap(([start, result]) => Effect.as(start, result)),
95+
Effect.uninterruptible,
96+
);
97+
return yield* result;
98+
});
99+
});
100+
};

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

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

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

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

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

11901196
// Connects run discovery under a timeout and may disconnect mid-scan. Neither
11911197
// may cancel the scan: on a busy host every connect would time out partway

0 commit comments

Comments
 (0)