Skip to content

Commit 9cdd251

Browse files
committed
perf(source-control): reduce pull request read overhead
1 parent c1d528a commit 9cdd251

15 files changed

Lines changed: 1334 additions & 298 deletions

File tree

‎apps/server/src/sourceControl/builtInDrivers.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli";
2525
import * as GitLabPullRequestCli from "@t3tools/source-control-gitlab/server/GitLabPullRequestCli";
2626
import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
2727
import type { SourceControlDriver } from "@t3tools/source-control-core/server/driver";
28+
import * as Undici from "@effect/platform-node/Undici";
29+
import * as Effect from "effect/Effect";
2830
import * as Layer from "effect/Layer";
31+
import { FetchHttpClient } from "effect/http";
2932

3033
import * as ServerSourceControlHost from "./ServerSourceControlHost.ts";
3134

@@ -47,6 +50,56 @@ export const BUILT_IN_SOURCE_CONTROL_DRIVERS: ReadonlyArray<
4750
SourceControlDriver<BuiltInSourceControlDriversEnv>
4851
> = drivers;
4952

53+
/**
54+
* The client the HTTP drivers read through: fetch, so redirects and headers behave as before, over
55+
* one pool of its own. The server's fetch drops a connection after four idle seconds and opens one
56+
* per concurrent request, so nearly every pull request opened after a pause paid a new TLS
57+
* handshake per read. This pool keeps connections for minutes, or for as long as the host does,
58+
* and carries a panel's parallel reads over one HTTP/2 connection where the host offers it.
59+
* Fresh, since the server builds the same `FetchHttpClient.layer` for itself and a memoized one
60+
* would ignore the `Fetch` given here.
61+
*/
62+
const httpDriverClient = Layer.fresh(FetchHttpClient.layer).pipe(
63+
Layer.provide(
64+
Layer.effect(FetchHttpClient.Fetch)(
65+
Effect.acquireRelease(
66+
Effect.sync(() => {
67+
const options = {
68+
allowH2: true,
69+
keepAliveTimeout: 300_000,
70+
keepAliveMaxTimeout: 300_000,
71+
};
72+
// Match Node's proxy opt-in, including flags supplied through NODE_OPTIONS.
73+
const proxyFlags = [
74+
...(process.env.NODE_OPTIONS?.match(/--(?:no-)?use-env-proxy\b/g) ?? []),
75+
...process.execArgv,
76+
];
77+
const proxyFlag = proxyFlags.findLast(
78+
(flag) => flag === "--use-env-proxy" || flag === "--no-use-env-proxy",
79+
);
80+
const useProxy =
81+
proxyFlag === undefined
82+
? process.env.NODE_USE_ENV_PROXY === "1"
83+
: proxyFlag === "--use-env-proxy";
84+
return useProxy ? new Undici.EnvHttpProxyAgent(options) : new Undici.Agent(options);
85+
}),
86+
(dispatcher) => Effect.promise(() => dispatcher.destroy()),
87+
).pipe(
88+
// Node's own fetch types and this undici's describe the same init and response, but name
89+
// a different `Dispatcher`, so they meet through `unknown`.
90+
Effect.map(
91+
(dispatcher): typeof globalThis.fetch =>
92+
(input, init) =>
93+
Undici.fetch(input as Undici.RequestInfo, {
94+
...(init as unknown as Undici.RequestInit),
95+
dispatcher,
96+
}) as unknown as Promise<Response>,
97+
),
98+
),
99+
),
100+
),
101+
);
102+
50103
/** The services the built-in drivers' packages own, plus the host port they all run against. */
51104
export const layer = Layer.mergeAll(
52105
// `GitHubApi.layerWithDependencies` carries the quota reserve and rate-limit pause every GitHub
@@ -57,4 +110,4 @@ export const layer = Layer.mergeAll(
57110
ForgejoCli.layer,
58111
GitCafeApi.layer.pipe(Layer.provideMerge(GitCafeCredentials.layer)),
59112
GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer)),
60-
).pipe(Layer.provideMerge(ServerSourceControlHost.layer));
113+
).pipe(Layer.provide(httpDriverClient), Layer.provideMerge(ServerSourceControlHost.layer));

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,9 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
801801
yield* ask();
802802
const again = yield* ask();
803803

804-
assert.strictEqual(mockedExecute.mock.calls.length, 5);
805-
// The second read goes straight to the pushes, and still answers with the head's blob.
804+
assert.strictEqual(mockedExecute.mock.calls.length, 4);
805+
// The second read goes straight to the pushes, finds the same push's changes already held,
806+
// and still answers with the head's blob.
806807
expect(argsOfCall(3)).toContain("pullRequestIterations");
807808
expect([...again.revisions]).toEqual([["README.md", "8f80"]]);
808809
}),

0 commit comments

Comments
 (0)