Skip to content

Commit ff9cb58

Browse files
committed
perf(client-runtime): start a replacement socket while a long-resume probe waits
After a long iOS background the old socket is usually dead, but the connect chain only started once the probe gave up. On remote routes a replacement session now starts 500 ms into the probe. A frame or probe answer on the old socket cancels it; a ready replacement swaps in and the next attempt adopts it instead of connecting again. LAN and loopback routes keep the previous behavior.
1 parent ddbe5d3 commit ff9cb58

3 files changed

Lines changed: 341 additions & 51 deletions

File tree

‎docs/internals/connection-runtime.md‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,22 @@ socket without a close event, so a probe still unanswered after 500 ms asks the
2323
host for its public descriptor over a fresh request, again each second until it
2424
answers. If the host answers while the socket delivers no frame, the socket is
2525
dead and is replaced, even while the network reports offline: at once on
26-
loopback and LAN, and 2.5 seconds after the check starts elsewhere, where one
27-
lost packet can stall a live socket. Without that evidence the probe deadline
28-
decides: 3 seconds on loopback and LAN routes and after a long mobile
29-
suspension, when the data on screen is stale; 15 seconds elsewhere, because a
30-
tailnet over DERP or a VPN can be slower than that on a live socket. A long
31-
suspension's probe that times out also replaces the socket regardless of the
32-
network report. The web and desktop foreground probe skips the fresh request,
26+
loopback and LAN and after a long mobile suspension, and 2.5 seconds after the
27+
check starts elsewhere, where one lost packet can stall a live socket. A frame
28+
during the check shows the socket is alive. Without that evidence the probe
29+
deadline decides: 3 seconds on loopback and LAN routes; 15 seconds elsewhere,
30+
because a tailnet over DERP or a VPN can be slower than that on a live socket;
31+
1.5 seconds after a long suspension, when the data on screen is stale and
32+
pooled connections are often dead too (3 seconds once a frame has arrived). A
33+
long suspension's probe that times out also replaces the socket regardless of
34+
the network report. Replacing a dead remote socket costs a full connect chain
35+
(descriptor, ticket, socket, server config), so after a long suspension on a
36+
remote route a probe still unanswered at 500 ms starts that chain beside it
37+
instead of after it. Whichever settles first wins: a probe answer or a frame on
38+
the old socket closes the replacement, a ready replacement swaps in at once, and
39+
a replacement that fails leaves the probe to decide. One still connecting when
40+
the old socket is given up on is adopted by the next attempt, never connected
41+
twice. The web and desktop foreground probe skips the fresh request,
3342
because a busy host can answer it before a queued probe. Foregrounding also
3443
wakes a pending retry immediately. A long suspension restarts an in-flight
3544
attempt; a short one leaves it alone.

‎packages/client-runtime/src/connection/supervisor.test.ts‎

Lines changed: 165 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,28 +1012,182 @@ describe("EnvironmentSupervisor", () => {
10121012
}).pipe(Effect.provide(TestClock.layer())),
10131013
);
10141014

1015-
it.effect("keeps a long-resume socket that delivers a frame during a slow host check", () =>
1015+
it.effect(
1016+
"keeps a long-resume socket that delivers a frame during a slow host check and closes the replacement",
1017+
() =>
1018+
Effect.gen(function* () {
1019+
const frames = yield* Ref.make(0);
1020+
const replacementRequested = yield* Deferred.make<void>();
1021+
const harness = yield* makeHarness({
1022+
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1023+
initialConfig: () => Effect.succeed(PROBE_CAPABLE_CONFIG),
1024+
probe: (attempt) => (attempt === 1 ? Effect.sleep("2500 millis") : Effect.void),
1025+
ready: (attempt) =>
1026+
attempt === 2
1027+
? Deferred.succeed(replacementRequested, undefined).pipe(Effect.andThen(Effect.never))
1028+
: Effect.void,
1029+
inboundFrames: Ref.get(frames),
1030+
checkHost: () => Effect.sleep("2 seconds").pipe(Effect.as("answered")),
1031+
});
1032+
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
1033+
initiallyDesired: true,
1034+
}).pipe(Effect.provide(harness.dependencies));
1035+
1036+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1037+
yield* harness.wake("application-active-long");
1038+
yield* TestClock.adjust("700 millis");
1039+
expect(yield* Deferred.isDone(replacementRequested)).toBe(true);
1040+
yield* Ref.update(frames, (count) => count + 1);
1041+
// The frame check polls every 100 ms, well before the probe answers.
1042+
yield* TestClock.adjust("100 millis");
1043+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1044+
yield* TestClock.adjust("5 seconds");
1045+
1046+
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
1047+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1048+
expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(1);
1049+
}).pipe(Effect.provide(TestClock.layer())),
1050+
);
1051+
1052+
const makeDeadRemoteSocketHarness = Effect.fn("test.makeDeadRemoteSocketHarness")(function* (
1053+
replacementReady: (
1054+
requested: Deferred.Deferred<void>,
1055+
) => Effect.Effect<void, ConnectionAttemptError>,
1056+
) {
1057+
const replacementRequested = yield* Deferred.make<void>();
1058+
const harness = yield* makeHarness({
1059+
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1060+
initialConfig: () => Effect.succeed(PROBE_CAPABLE_CONFIG),
1061+
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
1062+
ready: (attempt) =>
1063+
attempt === 2
1064+
? Deferred.succeed(replacementRequested, undefined).pipe(
1065+
Effect.andThen(replacementReady(replacementRequested)),
1066+
)
1067+
: Effect.void,
1068+
// A dead pooled connection hangs the host check along with the socket.
1069+
checkHost: () => Effect.never,
1070+
});
1071+
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
1072+
initiallyDesired: true,
1073+
}).pipe(Effect.provide(harness.dependencies));
1074+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1075+
return { harness, supervisor, replacementRequested };
1076+
});
1077+
1078+
it.effect(
1079+
"swaps to a replacement that is ready before the probe of a dead remote socket gives up",
1080+
() =>
1081+
Effect.gen(function* () {
1082+
const ready = yield* Deferred.make<void>();
1083+
const { harness, supervisor, replacementRequested } = yield* makeDeadRemoteSocketHarness(
1084+
() => Deferred.await(ready),
1085+
);
1086+
1087+
yield* harness.wake("application-active-long");
1088+
yield* TestClock.adjust("499 millis");
1089+
expect(yield* Deferred.isDone(replacementRequested)).toBe(false);
1090+
yield* TestClock.adjust("1 millis");
1091+
expect(yield* Deferred.isDone(replacementRequested)).toBe(true);
1092+
// The old session keeps serving until the replacement is ready.
1093+
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
1094+
phase: "connected",
1095+
generation: 1,
1096+
});
1097+
1098+
yield* Deferred.succeed(ready, undefined);
1099+
yield* awaitState(
1100+
supervisor.state,
1101+
(state) => state.phase === "connected" && state.generation === 2,
1102+
);
1103+
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
1104+
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
1105+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1106+
}).pipe(Effect.provide(TestClock.layer())),
1107+
);
1108+
1109+
it.effect("adopts a replacement that is still connecting when the probe gives up", () =>
1110+
Effect.gen(function* () {
1111+
const ready = yield* Deferred.make<void>();
1112+
const { harness, supervisor, replacementRequested } = yield* makeDeadRemoteSocketHarness(() =>
1113+
Deferred.await(ready),
1114+
);
1115+
1116+
yield* harness.wake("application-active-long");
1117+
yield* TestClock.adjust("1500 millis");
1118+
expect(yield* Deferred.isDone(replacementRequested)).toBe(true);
1119+
yield* Deferred.succeed(ready, undefined);
1120+
yield* awaitState(
1121+
supervisor.state,
1122+
(state) => state.phase === "connected" && state.generation === 2,
1123+
);
1124+
1125+
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
1126+
expect(yield* Ref.get(harness.sessionCount)).toBe(2);
1127+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1128+
}).pipe(Effect.provide(TestClock.layer())),
1129+
);
1130+
1131+
it.effect("leaves a dead remote socket to its probe when the replacement fails", () =>
1132+
Effect.gen(function* () {
1133+
const { harness, supervisor } = yield* makeDeadRemoteSocketHarness(() =>
1134+
Effect.fail(transient()),
1135+
);
1136+
1137+
yield* harness.wake("application-active-long");
1138+
yield* TestClock.adjust("1499 millis");
1139+
expect(yield* SubscriptionRef.get(supervisor.state)).toMatchObject({
1140+
phase: "connected",
1141+
generation: 1,
1142+
});
1143+
expect(yield* Ref.get(harness.releaseCount)).toBe(1);
1144+
1145+
yield* TestClock.adjust("1 millis");
1146+
yield* awaitState(
1147+
supervisor.state,
1148+
(state) => state.phase === "connected" && state.generation === 2,
1149+
);
1150+
expect(yield* Ref.get(harness.prepareCount)).toBe(3);
1151+
expect(yield* Ref.get(harness.releaseCount)).toBe(2);
1152+
}).pipe(Effect.provide(TestClock.layer())),
1153+
);
1154+
1155+
it.effect.each([
1156+
{ name: "LAN", entry: LAN_ENTRY },
1157+
{
1158+
name: "loopback",
1159+
entry: {
1160+
...TARGET_ENTRY,
1161+
target: new PrimaryConnectionTarget({
1162+
...TARGET,
1163+
httpBaseUrl: "http://127.0.0.1:3773",
1164+
wsBaseUrl: "ws://127.0.0.1:3773",
1165+
}),
1166+
},
1167+
},
1168+
])("waits for the probe of a dead $name socket after a long resume", ({ entry }) =>
10161169
Effect.gen(function* () {
1017-
const frames = yield* Ref.make(0);
10181170
const harness = yield* makeHarness({
10191171
prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
10201172
initialConfig: () => Effect.succeed(PROBE_CAPABLE_CONFIG),
1021-
probe: (attempt) => (attempt === 1 ? Effect.sleep("2500 millis") : Effect.void),
1022-
inboundFrames: Ref.get(frames),
1023-
checkHost: () => Effect.sleep("2 seconds").pipe(Effect.as("answered")),
1173+
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
1174+
checkHost: () => Effect.succeed("silent"),
10241175
});
1025-
const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
1176+
const supervisor = yield* EnvironmentSupervisor.make(entry, {
10261177
initiallyDesired: true,
10271178
}).pipe(Effect.provide(harness.dependencies));
10281179

10291180
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
10301181
yield* harness.wake("application-active-long");
1031-
yield* TestClock.adjust("700 millis");
1032-
yield* Ref.update(frames, (count) => count + 1);
1033-
yield* TestClock.adjust("5 seconds");
1182+
yield* TestClock.adjust("1499 millis");
1183+
expect(yield* Ref.get(harness.prepareCount)).toBe(1);
10341184

1035-
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
1036-
expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(1);
1185+
yield* TestClock.adjust("1 millis");
1186+
yield* awaitState(
1187+
supervisor.state,
1188+
(state) => state.phase === "connected" && state.generation === 2,
1189+
);
1190+
expect(yield* Ref.get(harness.prepareCount)).toBe(2);
10371191
}).pipe(Effect.provide(TestClock.layer())),
10381192
);
10391193

0 commit comments

Comments
 (0)