Skip to content

Commit 50e187a

Browse files
perf(mobile): warm T3 Connect credentials before the socket opens
Prefetch DPoP tokens on cold start and app wake for recent environments, mint the preferred relay ticket while route checks run, and skip the LAN head-start when no LAN route remains. Co-authored-by: Cursor <[email protected]>
1 parent f0dd64a commit 50e187a

7 files changed

Lines changed: 232 additions & 14 deletions

File tree

‎packages/client-runtime/src/connection/driver.ts‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import type {
2121
import { ConnectionTransientError } from "./model.ts";
2222
import * as ConnectionResolver from "./resolver.ts";
2323
import {
24+
connectionRouteId,
2425
connectionRoutes,
2526
routeEntry,
2627
routeHttpBaseUrl,
28+
routeUsesDpop,
2729
routesForNetwork,
30+
routesIncludeLan,
2831
sendsTokenInClear,
2932
} from "./routes.ts";
3033
import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
@@ -83,11 +86,15 @@ export const connectOverRoutes = Effect.fn("ConnectionDriver.connectOverRoutes")
8386
privateNetworkLikely = true,
8487
) {
8588
const routes = routesForNetwork(entry, privateNetworkLikely);
86-
const checks =
87-
routes.length === 1
88-
? []
89-
: yield* Effect.forEach(routes, (route) => Effect.forkChild(checkRoute(route)));
90-
const headStart = yield* Effect.forkChild(Effect.sleep(ROUTE_CHECK_HEAD_START_MS));
89+
// Reachability checks exist to avoid opening a dead LAN before relay. With no
90+
// LAN in the race, start connecting immediately and let the hedge pick a winner.
91+
const classifyFirst = routes.length > 1 && routesIncludeLan(routes);
92+
const checks = classifyFirst
93+
? yield* Effect.forEach(routes, (route) => Effect.forkChild(checkRoute(route)))
94+
: [];
95+
const headStart = classifyFirst
96+
? yield* Effect.forkChild(Effect.sleep(ROUTE_CHECK_HEAD_START_MS))
97+
: undefined;
9198
const attemptScope = yield* Scope.Scope;
9299
let transient: ConnectionAttemptError | undefined;
93100
let blocked: ConnectionAttemptError | undefined;
@@ -184,7 +191,7 @@ export const connectOverRoutes = Effect.fn("ConnectionDriver.connectOverRoutes")
184191
const ready: Array<ConnectionRoute> = [];
185192
for (const [index, route] of routes.entries()) {
186193
const check =
187-
checks.length === 0
194+
headStart === undefined || checks.length === 0
188195
? "unchecked"
189196
: yield* Effect.raceFirst(
190197
Fiber.join(checks[index]!),
@@ -275,11 +282,34 @@ export const make = Effect.gen(function* () {
275282
});
276283
yield* reportProgress({ stage: "preparing" });
277284
const privateNetworkLikely = yield* connectivity.privateNetworkLikely;
285+
const routes = routesForNetwork(entry, privateNetworkLikely);
286+
const preferred = routes[0];
287+
// Mint the DPoP ticket while route checks run so T3 Connect is not strictly
288+
// serialized behind the LAN head-start window.
289+
const preferredPrepare =
290+
preferred !== undefined && routeUsesDpop(preferred)
291+
? yield* Effect.forkChild(
292+
resolver.prepare(routeEntry(entry, preferred)).pipe(Effect.result),
293+
)
294+
: undefined;
295+
const preferredRouteId =
296+
preferred === undefined ? undefined : connectionRouteId(preferred.target);
278297
return yield* connectOverRoutes(
279298
entry,
280299
(route) => checkRoute(entry, route),
281300
Effect.fnUntraced(function* (route) {
282-
const prepared = yield* resolver.prepare(routeEntry(entry, route));
301+
const prepared =
302+
preferredPrepare !== undefined &&
303+
preferredRouteId !== undefined &&
304+
connectionRouteId(route.target) === preferredRouteId
305+
? yield* Fiber.join(preferredPrepare).pipe(
306+
Effect.flatMap((result) =>
307+
result._tag === "Success"
308+
? Effect.succeed(result.success)
309+
: Effect.fail(result.failure),
310+
),
311+
)
312+
: yield* resolver.prepare(routeEntry(entry, route));
283313
yield* reportProgress({ stage: "opening", prepared });
284314
const session = yield* sessions.connect(prepared);
285315
yield* reportProgress({ stage: "synchronizing", prepared });

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
import * as EnvironmentRegistry from "./registry.ts";
4040
import * as ConnectionProfileStore from "./profileStore.ts";
4141
import { connectionRoutes, routeHttpBaseUrl } from "./routes.ts";
42+
import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
4243
import * as ConnectionWakeups from "./wakeups.ts";
4344

4445
const layerClientPresentation = Layer.succeed(
@@ -196,6 +197,19 @@ function layerPairingOnboarding(
196197
ConnectionWakeups.ConnectionWakeups,
197198
ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.never }),
198199
),
200+
Layer.mock(RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization)({
201+
authorizeDpopHttp: (input) =>
202+
Effect.succeed({
203+
environmentId: input.expectedEnvironmentId,
204+
label: "Prefetched",
205+
httpBaseUrl: "https://relay.example.test",
206+
httpAuthorization: {
207+
_tag: "Dpop" as const,
208+
accessToken: "prefetch-token",
209+
expiresAtEpochMs: Number.MAX_SAFE_INTEGER,
210+
},
211+
}),
212+
}),
199213
);
200214
return Onboarding.layer.pipe(
201215
Layer.provideMerge(EnvironmentRegistry.layer.pipe(Layer.provideMerge(dependencies))),

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as Exit from "effect/Exit";
1414
import * as Fiber from "effect/Fiber";
1515
import * as Layer from "effect/Layer";
1616
import * as Option from "effect/Option";
17+
import * as Queue from "effect/Queue";
1718
import * as Ref from "effect/Ref";
1819
import * as Result from "effect/Result";
1920
import * as Scheduler from "effect/Scheduler";
@@ -23,6 +24,7 @@ import * as SubscriptionRef from "effect/SubscriptionRef";
2324
import * as TestClock from "effect/testing/TestClock";
2425
import * as Tracer from "effect/Tracer";
2526

27+
import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
2628
import * as ClientCapabilities from "../platform/capabilities.ts";
2729
import * as TokenStore from "../authorization/tokenStore.ts";
2830
import {
@@ -172,6 +174,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
172174
readonly environmentId: EnvironmentId;
173175
readonly atEpochMs: number;
174176
}>;
177+
readonly onDpopPrefetch?: (environmentId: EnvironmentId) => Effect.Effect<void>;
175178
readonly directEndpoints?: ServerConfig["directEndpoints"];
176179
},
177180
) {
@@ -345,6 +348,33 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
345348
changes: SubscriptionRef.changes(networkStatus),
346349
privateNetworkLikely: Effect.succeed(true),
347350
});
351+
const dpopPrefetchCalls = yield* Ref.make(0);
352+
const wakeups = yield* SubscriptionRef.make<{
353+
readonly sequence: number;
354+
readonly reason: ConnectionWakeups.ConnectionWakeup;
355+
}>({
356+
sequence: 0,
357+
reason: "application-active",
358+
});
359+
const remoteAuthorization = RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization.of({
360+
authorizeBearer: () => Effect.die("unused"),
361+
authorizeDpop: () => Effect.die("unused"),
362+
authorizeDpopHttp: (input) =>
363+
Ref.update(dpopPrefetchCalls, (count) => count + 1).pipe(
364+
Effect.andThen(options?.onDpopPrefetch?.(input.expectedEnvironmentId) ?? Effect.void),
365+
Effect.as({
366+
environmentId: input.expectedEnvironmentId,
367+
label: "Prefetched",
368+
httpBaseUrl: "https://relay.example.test",
369+
httpAuthorization: {
370+
_tag: "Dpop" as const,
371+
accessToken: "prefetch-token",
372+
expiresAtEpochMs: Number.MAX_SAFE_INTEGER,
373+
},
374+
}),
375+
),
376+
renewDpopBeforeExpiry: () => Effect.die("unused"),
377+
});
348378
const profileStore = ConnectionProfileStore.ConnectionProfileStore.of({
349379
get: (connectionId) =>
350380
Ref.update(profileReadCount, (count) => count + 1).pipe(
@@ -500,7 +530,16 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
500530
Layer.succeed(Connectivity.Connectivity, connectivity),
501531
Layer.succeed(
502532
ConnectionWakeups.ConnectionWakeups,
503-
ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.never }),
533+
ConnectionWakeups.ConnectionWakeups.of({
534+
changes: SubscriptionRef.changes(wakeups).pipe(
535+
Stream.drop(1),
536+
Stream.map((event) => event.reason),
537+
),
538+
}),
539+
),
540+
Layer.succeed(
541+
RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
542+
remoteAuthorization,
504543
),
505544
Layer.succeed(ConnectionDriver.ConnectionDriver, driver),
506545
layerCache,
@@ -526,6 +565,12 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
526565
disconnectedSshTargets,
527566
networkStatus,
528567
connectedRoutes,
568+
dpopPrefetchCalls,
569+
wake: (reason: ConnectionWakeups.ConnectionWakeup) =>
570+
SubscriptionRef.update(wakeups, (event) => ({
571+
sequence: event.sequence + 1,
572+
reason,
573+
})),
529574
};
530575
});
531576

@@ -717,6 +762,31 @@ describe("EnvironmentRegistry", () => {
717762
}),
718763
);
719764

765+
it.effect(
766+
"prefetches T3 Connect credentials on start and wake for recent relay environments",
767+
() =>
768+
Effect.gen(function* () {
769+
const prefetches = yield* Queue.unbounded<EnvironmentId>();
770+
const harness = yield* makeHarness([RELAY_TARGET], [], [], {
771+
onDpopPrefetch: (environmentId) => Queue.offer(prefetches, environmentId),
772+
});
773+
774+
yield* Effect.gen(function* () {
775+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
776+
yield* registry.start;
777+
expect(yield* Queue.take(prefetches)).toBe(RELAY_TARGET.environmentId);
778+
yield* awaitConnectionState(
779+
registry,
780+
RELAY_TARGET.environmentId,
781+
(state) => state.phase === "connected",
782+
);
783+
784+
yield* harness.wake("application-active");
785+
expect(yield* Queue.take(prefetches)).toBe(RELAY_TARGET.environmentId);
786+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
787+
}),
788+
);
789+
720790
it.effect("does not expose a persisted environment as disconnected while it starts", () =>
721791
Effect.gen(function* () {
722792
const connectionStarted = yield* Deferred.make<void>();

‎packages/client-runtime/src/connection/registry.ts‎

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as Semaphore from "effect/Semaphore";
1414
import * as Stream from "effect/Stream";
1515
import * as SubscriptionRef from "effect/SubscriptionRef";
1616

17+
import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
1718
import * as ClientCapabilities from "../platform/capabilities.ts";
1819
import {
1920
type ConnectionCatalogEntry,
@@ -49,6 +50,7 @@ import {
4950
connectionRouteId,
5051
connectionRoutes,
5152
credentialConnectionId,
53+
entryUsesDpop,
5254
entryWithRoutes,
5355
findRouteToSameAddress,
5456
hasSavedBearerRoute,
@@ -227,6 +229,7 @@ export const make = Effect.gen(function* () {
227229
const connectivity = yield* Connectivity.Connectivity;
228230
const driver = yield* ConnectionDriver.ConnectionDriver;
229231
const wakeups = yield* ConnectionWakeups.ConnectionWakeups;
232+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
230233
const ssh = yield* ClientCapabilities.SshEnvironmentGateway;
231234
const persistedTargets = yield* storage.list;
232235
const disabledEnvironmentIds = new Set(yield* storage.listDisabled);
@@ -482,18 +485,58 @@ export const make = Effect.gen(function* () {
482485
Effect.catchTags({ EnvironmentNotRegisteredError: () => Effect.void }),
483486
);
484487

485-
const start = Effect.gen(function* () {
486-
if (yield* Ref.getAndSet(started, true)) {
487-
return;
488-
}
489-
const now = yield* Clock.currentTimeMillis;
490-
const lastConnectedById = new Map(
488+
const loadLastConnectedById = Effect.fnUntraced(function* () {
489+
return new Map(
491490
(yield* storage.listLastConnected.pipe(
492491
Effect.orElseSucceed(
493492
(): ReadonlyArray<{ environmentId: EnvironmentId; atEpochMs: number }> => [],
494493
),
495494
)).map((row) => [row.environmentId, row.atEpochMs] as const),
496495
);
496+
});
497+
498+
/**
499+
* Warms T3 Connect access tokens before (or while) supervisors open sockets,
500+
* so prepare often hits the token cache instead of a full bootstrap exchange.
501+
*/
502+
const prefetchDpopCredentials = Effect.fn("EnvironmentRegistry.prefetchDpopCredentials")(
503+
function* () {
504+
const now = yield* Clock.currentTimeMillis;
505+
const lastConnectedById = yield* loadLastConnectedById();
506+
const current = yield* SubscriptionRef.get(entries);
507+
yield* Effect.forEach(
508+
current.values(),
509+
(entry) => {
510+
if (!entry.enabled || entry.unsupportedReason !== undefined || !entryUsesDpop(entry)) {
511+
return Effect.void;
512+
}
513+
const lastConnectedAt = lastConnectedById.get(entry.target.environmentId);
514+
if (lastConnectedAt !== undefined && now - lastConnectedAt >= STALE_RECONNECT_AFTER_MS) {
515+
return Effect.void;
516+
}
517+
return remote
518+
.authorizeDpopHttp({ expectedEnvironmentId: entry.target.environmentId })
519+
.pipe(
520+
Effect.asVoid,
521+
Effect.catch((error) =>
522+
Effect.logDebug("Could not prefetch T3 Connect credentials.", {
523+
environmentId: entry.target.environmentId,
524+
error,
525+
}),
526+
),
527+
);
528+
},
529+
{ concurrency: 2, discard: true },
530+
);
531+
},
532+
);
533+
534+
const start = Effect.gen(function* () {
535+
if (yield* Ref.getAndSet(started, true)) {
536+
return;
537+
}
538+
const now = yield* Clock.currentTimeMillis;
539+
const lastConnectedById = yield* loadLastConnectedById();
497540
const fresh: EnvironmentId[] = [];
498541
const stale: EnvironmentId[] = [];
499542
for (const environmentId of persistedRoutesByEnvironment.keys()) {
@@ -505,6 +548,8 @@ export const make = Effect.gen(function* () {
505548
fresh.push(environmentId);
506549
}
507550
}
551+
// Overlap token mint with supervisor connect for recently used T3 Connect envs.
552+
yield* prefetchDpopCredentials().pipe(Effect.forkIn(registryScope));
508553
yield* Effect.forEach(fresh, startEnvironment, {
509554
concurrency: "unbounded",
510555
discard: true,
@@ -1221,6 +1266,17 @@ export const make = Effect.gen(function* () {
12211266
Stream.runForEach((status) => SubscriptionRef.set(networkStatus, status)),
12221267
Effect.forkScoped,
12231268
);
1269+
yield* wakeups.changes.pipe(
1270+
Stream.filter(
1271+
(reason) =>
1272+
reason === "application-active" ||
1273+
reason === "application-active-probe" ||
1274+
reason === "application-active-long" ||
1275+
reason === "network-changed",
1276+
),
1277+
Stream.runForEach(() => prefetchDpopCredentials()),
1278+
Effect.forkScoped,
1279+
);
12241280

12251281
const setCompatibility = Effect.fn("EnvironmentRegistry.setCompatibility")(function* (
12261282
environmentId: EnvironmentId,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import {
2020
pendingRouteOrder,
2121
preferRoute,
2222
removedWithRelay,
23+
routeUsesDpop,
2324
routesAfterRemoving,
2425
routesForNetwork,
26+
routesIncludeLan,
2527
sendsTokenInClear,
2628
upsertRoute,
2729
usableRoutes,
@@ -102,6 +104,10 @@ describe("connection routes", () => {
102104
};
103105
expect(routesForNetwork(entry, true)).toEqual([LAN, TAILNET, RELAY]);
104106
expect(routesForNetwork(entry, false)).toEqual([TAILNET, RELAY]);
107+
expect(routesIncludeLan([LAN, TAILNET])).toBe(true);
108+
expect(routesIncludeLan([TAILNET, RELAY])).toBe(false);
109+
expect(routeUsesDpop(RELAY)).toBe(true);
110+
expect(routeUsesDpop(LAN)).toBe(false);
105111
});
106112

107113
it("keeps LAN when it is the only saved route", () => {

‎packages/client-runtime/src/connection/routes.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,21 @@ export function routesForNetwork(
381381
return withoutLan.length > 0 ? withoutLan : routes;
382382
}
383383

384+
/** T3 Connect relay and learned/saved t3-connect bearer routes need a DPoP access token. */
385+
export function routeUsesDpop(route: ConnectionRoute): boolean {
386+
if (route.target._tag === "RelayConnectionTarget") return true;
387+
const profile = Option.getOrNull(route.profile);
388+
return profile?._tag === "BearerConnectionProfile" && profile.authorization === "t3-connect";
389+
}
390+
391+
export function entryUsesDpop(entry: ConnectionCatalogEntry): boolean {
392+
return connectionRoutes(entry).some(routeUsesDpop);
393+
}
394+
395+
export function routesIncludeLan(routes: ReadonlyArray<ConnectionRoute>): boolean {
396+
return routes.some((route) => connectionRouteKind(route) === "lan");
397+
}
398+
384399
export interface ReportedEndpoint {
385400
readonly httpBaseUrl: string;
386401
readonly kind?: string;

0 commit comments

Comments
 (0)