Skip to content

Commit c003652

Browse files
committed
perf(mobile): reconnect reuses the descriptor its host check just read
Resuming from the background checks the host with a descriptor GET, then the reconnect read the same descriptor again before asking for a ticket. The host check now hands its answer to the next direct authorization of that address, once and only within 5 seconds.
1 parent a005905 commit c003652

5 files changed

Lines changed: 81 additions & 7 deletions

File tree

‎packages/client-runtime/src/authorization/layer.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,49 @@ describe("RemoteEnvironmentAuthorization", () => {
317317
}),
318318
);
319319

320+
it.effect("reuses a descriptor a host check just read, once and only while fresh", () =>
321+
Effect.gen(function* () {
322+
const descriptor = {
323+
...DESCRIPTOR,
324+
orchestrationProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION,
325+
};
326+
const harness = yield* makeHarness({
327+
responses: [
328+
websocketTicket("first-ticket"),
329+
Response.json(descriptor),
330+
websocketTicket("second-ticket"),
331+
Response.json(descriptor),
332+
websocketTicket("third-ticket"),
333+
],
334+
});
335+
336+
yield* Effect.gen(function* () {
337+
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
338+
const authorize = remote.authorizeBearer({
339+
expectedEnvironmentId: ENVIRONMENT_ID,
340+
httpBaseUrl: ENDPOINT.httpBaseUrl,
341+
wsBaseUrl: ENDPOINT.wsBaseUrl,
342+
bearerToken: "bearer-token",
343+
connectionMethod: "direct",
344+
});
345+
yield* remote.rememberCheckedDescriptor(ENDPOINT.httpBaseUrl, descriptor as never);
346+
yield* authorize;
347+
yield* authorize;
348+
yield* remote.rememberCheckedDescriptor(ENDPOINT.httpBaseUrl, descriptor as never);
349+
yield* TestClock.adjust("6 seconds");
350+
yield* authorize;
351+
}).pipe(Effect.provide(harness.layer));
352+
353+
expect(harness.fetch.calls.map(([url]) => new URL(String(url)).pathname)).toEqual([
354+
"/api/auth/websocket-ticket/rpc",
355+
"/.well-known/t3/environment",
356+
"/api/auth/websocket-ticket/rpc",
357+
"/.well-known/t3/environment",
358+
"/api/auth/websocket-ticket/rpc",
359+
]);
360+
}),
361+
);
362+
320363
it.effect("blocks a warm direct bearer prepare when the environment changes identity", () =>
321364
Effect.gen(function* () {
322365
const harness = yield* makeHarness({

‎packages/client-runtime/src/authorization/service.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
8282
readonly expectedEnvironmentId: EnvironmentId;
8383
readonly rejectedAccessToken?: string;
8484
}) => Effect.Effect<AuthorizedRemoteHttpEnvironment, ConnectionAttemptError>;
85+
/** Lets the next direct authorization of this address skip its own descriptor read. */
86+
readonly rememberCheckedDescriptor: (
87+
httpBaseUrl: string,
88+
descriptor: ExecutionEnvironmentDescriptor,
89+
) => Effect.Effect<void>;
8590
/**
8691
* Renews a live socket's token before it expires, so the server moves the
8792
* socket to the new session instead of closing it. Runs until interrupted.
@@ -95,6 +100,7 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
95100

96101
const CACHED_ENDPOINT_SOCKET_TIMEOUT_MS = 3_000;
97102
const DPOP_AUTHORIZATION_TIMEOUT_MS = 30_000;
103+
const CHECKED_DESCRIPTOR_TTL_MS = 5_000;
98104

99105
function mapDpopSocketError(error: RemoteEnvironmentAuthError | ConnectionAttemptError) {
100106
return error._tag === "ConnectionTransientError" || error._tag === "ConnectionBlockedError"
@@ -135,6 +141,13 @@ export const make = Effect.gen(function* () {
135141
}
136142
>();
137143

144+
// A host check that just answered hands its descriptor to the reconnect it
145+
// triggers, once, so the reconnect skips a round trip.
146+
const checkedDescriptors = new Map<
147+
string,
148+
{ readonly descriptor: ExecutionEnvironmentDescriptor; readonly at: number }
149+
>();
150+
138151
/**
139152
* A saved LAN address can belong to another machine on a different network.
140153
*/
@@ -144,9 +157,15 @@ export const make = Effect.gen(function* () {
144157
readonly httpBaseUrl: string;
145158
readonly connectionMethod: ClientConnectionMethod;
146159
}) {
147-
const descriptor = yield* fetchDescriptor(input.httpBaseUrl, input.connectionMethod).pipe(
148-
Effect.provideService(HttpClient.HttpClient, httpClient),
149-
);
160+
const checked = checkedDescriptors.get(input.httpBaseUrl);
161+
checkedDescriptors.delete(input.httpBaseUrl);
162+
const descriptor =
163+
checked !== undefined &&
164+
(yield* Clock.currentTimeMillis) - checked.at <= CHECKED_DESCRIPTOR_TTL_MS
165+
? checked.descriptor
166+
: yield* fetchDescriptor(input.httpBaseUrl, input.connectionMethod).pipe(
167+
Effect.provideService(HttpClient.HttpClient, httpClient),
168+
);
150169
if (descriptor.environmentId !== input.expectedEnvironmentId) {
151170
return yield* environmentMismatchError({
152171
expected: input.expectedEnvironmentId,
@@ -527,6 +546,10 @@ export const make = Effect.gen(function* () {
527546

528547
return RemoteEnvironmentAuthorization.of({
529548
authorizeBearer,
549+
// Untraced: it runs on every live session's host check.
550+
rememberCheckedDescriptor: Effect.fnUntraced(function* (httpBaseUrl, descriptor) {
551+
checkedDescriptors.set(httpBaseUrl, { descriptor, at: yield* Clock.currentTimeMillis });
552+
}),
530553
authorizeDpop: Effect.fn("environment.authorization")(function (input) {
531554
return authorizeDpop(input);
532555
}),

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,18 @@ export const make = Effect.gen(function* () {
147147

148148
// The descriptor is public, so this sends no credential to whatever
149149
// answers at a saved LAN address on a different network.
150-
const checkDescriptor = (httpBaseUrl: string, environmentId: string) =>
150+
const checkDescriptor = (httpBaseUrl: string, environmentId: string, remember = false) =>
151151
fetchRemoteEnvironmentDescriptor({
152152
httpBaseUrl,
153153
timeoutMs: ROUTE_CHECK_TIMEOUT_MS,
154154
}).pipe(
155-
Effect.map((descriptor): RouteCheck =>
156-
descriptor.environmentId === environmentId ? "answered" : "silent",
155+
Effect.flatMap((descriptor) =>
156+
descriptor.environmentId !== environmentId
157+
? Effect.succeed<RouteCheck>("silent")
158+
: (remember
159+
? remote.rememberCheckedDescriptor(httpBaseUrl, descriptor)
160+
: Effect.void
161+
).pipe(Effect.as<RouteCheck>("answered")),
157162
),
158163
Effect.orElseSucceed((): RouteCheck => "silent"),
159164
// A cached descriptor would answer for a host that cannot be reached.
@@ -171,8 +176,9 @@ export const make = Effect.gen(function* () {
171176
);
172177
};
173178

179+
// Only a live session's host check feeds the reconnect it may trigger.
174180
const checkHost = (prepared: PreparedConnection) =>
175-
checkDescriptor(prepared.httpBaseUrl, prepared.environmentId).pipe(
181+
checkDescriptor(prepared.httpBaseUrl, prepared.environmentId, true).pipe(
176182
Effect.withSpan("ConnectionDriver.checkHost", {
177183
attributes: { "connection.target.kind": prepared.target._tag },
178184
}),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
108108
},
109109
})),
110110
authorizeDpopHttp: () => Effect.die("unused"),
111+
rememberCheckedDescriptor: () => Effect.void,
111112
renewDpopBeforeExpiry: () => Effect.die("unused"),
112113
});
113114
const ssh = ClientCapabilities.SshEnvironmentGateway.of({

‎packages/client-runtime/src/state/environmentHttpAuth.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ function makeHarness(reply: (requestNumber: number) => Response | Promise<Respon
146146
},
147147
};
148148
}),
149+
rememberCheckedDescriptor: () => Effect.void,
149150
renewDpopBeforeExpiry: () => Effect.die("HTTP requests must not renew a socket's token."),
150151
});
151152
const signer = ManagedRelay.ManagedRelayDpopSigner.of({

0 commit comments

Comments
 (0)