Skip to content

Commit f460136

Browse files
authored
perf(server): avoid building unused peer HTTP clients (#28405)
1 parent b0d2a86 commit f460136

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎apps/server/src/peer/PeerLinks.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { NodeHttpServer } from "@effect/platform-node";
22
import { type ExecutionEnvironmentDescriptor, OrchestratorMcpFailure } from "@t3tools/contracts";
33
import { expect, it } from "@effect/vitest";
44
import * as Context from "effect/Context";
5+
import * as Deferred from "effect/Deferred";
56
import * as Duration from "effect/Duration";
67
import * as Effect from "effect/Effect";
8+
import * as Fiber from "effect/Fiber";
79
import * as Layer from "effect/Layer";
810
import * as Option from "effect/Option";
911
import * as Ref from "effect/Ref";
1012
import * as Schema from "effect/Schema";
13+
import * as HttpRouter from "effect/http/HttpRouter";
1114
import * as SqlClient from "effect/sql/SqlClient";
1215
import * as TestClock from "effect/testing/TestClock";
1316
import { Tool, Toolkit } from "effect/ai";
@@ -339,6 +342,57 @@ it.effect("never sends a link's token to an address that stops answering as that
339342
).pipe(Effect.provide(NodeHttpServer.layerTest)),
340343
);
341344

345+
it.effect("cancelling a descriptor probe aborts its request and leaves later resolves fresh", () =>
346+
Effect.scoped(
347+
Effect.gen(function* () {
348+
const hold = yield* Ref.make(false);
349+
const requests = yield* Ref.make(0);
350+
const entered = yield* Deferred.make<void>();
351+
const interrupted = yield* Deferred.make<void>();
352+
const holdDescriptor = HttpRouter.middleware(
353+
(httpEffect) =>
354+
Effect.gen(function* () {
355+
const request = yield* HttpServerRequest.HttpServerRequest;
356+
if (request.url.startsWith("/.well-known/t3/environment")) {
357+
yield* Ref.update(requests, (count) => count + 1);
358+
if (yield* Ref.get(hold)) {
359+
return yield* Deferred.succeed(entered, undefined).pipe(
360+
Effect.andThen(Effect.never),
361+
Effect.onInterrupt(() =>
362+
Deferred.succeed(interrupted, undefined).pipe(Effect.asVoid),
363+
),
364+
Effect.interruptible,
365+
);
366+
}
367+
}
368+
return yield* httpEffect;
369+
}),
370+
{ global: true },
371+
);
372+
const b = yield* servePeer(
373+
box,
374+
Layer.merge(McpHttpServer.toolkitRegistration(ProbeToolkit, probeHandlers), holdDescriptor),
375+
);
376+
const a = yield* makeA(laptop);
377+
const pairing = yield* b.auth.issuePairingCredential();
378+
yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" });
379+
yield* Ref.set(b.bearers, []);
380+
yield* Ref.set(requests, 0);
381+
yield* Ref.set(hold, true);
382+
383+
const resolving = yield* a.links.resolve(box.environmentId).pipe(Effect.forkScoped);
384+
yield* Deferred.await(entered);
385+
yield* Fiber.interrupt(resolving);
386+
yield* Deferred.await(interrupted);
387+
388+
yield* Ref.set(hold, false);
389+
expect((yield* a.links.resolve(box.environmentId)).url).toBe(b.url);
390+
expect(yield* Ref.get(requests)).toBe(2);
391+
expect(yield* Ref.get(b.bearers)).toEqual([]);
392+
}),
393+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
394+
);
395+
342396
it.effect("reads one stored link without asking the peer", () =>
343397
Effect.scoped(
344398
Effect.gen(function* () {

‎apps/server/src/peer/PeerLinks.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ import * as Schema from "effect/Schema";
2727
import * as SqlClient from "effect/sql/SqlClient";
2828
import * as FetchHttpClient from "effect/http/FetchHttpClient";
2929
import * as HttpClient from "effect/http/HttpClient";
30+
import * as HttpApi from "effect/http-api/HttpApi";
3031
import * as HttpApiClient from "effect/http-api/HttpApiClient";
3132
import { isLocalLoopbackHost, isTailnetHost } from "@t3tools/shared/hostClassification";
3233

3334
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
3435
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
3536
import * as ServerSettings from "../serverSettings.ts";
3637

38+
const PeerHttpApi = HttpApi.make(EnvironmentHttpApi.identifier)
39+
.add(EnvironmentHttpApi.groups.metadata)
40+
.add(EnvironmentHttpApi.groups.mcpOAuth)
41+
.annotateMerge(EnvironmentHttpApi.annotations);
42+
3743
/** The redirect registered for a link. Nothing ever listens there: the code comes back in the response. */
3844
const LINK_REDIRECT_URI = "http://127.0.0.1/t3code-peer-link";
3945
/**
@@ -185,7 +191,7 @@ const make = Effect.gen(function* () {
185191
);
186192

187193
const clientFor = (origin: string) =>
188-
HttpApiClient.make(EnvironmentHttpApi, { baseUrl: origin }).pipe(
194+
HttpApiClient.make(PeerHttpApi, { baseUrl: origin }).pipe(
189195
Effect.provideService(HttpClient.HttpClient, peerHttpClient),
190196
);
191197

0 commit comments

Comments
 (0)