Skip to content

Commit e4d9824

Browse files
committed
perf(server): 跳过没有候选端口的空闲扫描
将 Windows 已有的空闲保护应用到 Linux 和 macOS,避免无意义的周期性 lsof 进程。 覆盖终端注册、移除及仅配置 URL 的恢复路径,27 项测试及服务器类型检查通过。
1 parent 13aa882 commit e4d9824

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

‎apps/server/src/preview/PortScanner.test.ts‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ effectIt.effect("does not swallow process probe defects", () =>
655655
const defect = new Error("unexpected process probe defect");
656656
const layer = layerProbeFailure(() => Effect.die(defect));
657657

658-
const exit = yield* Effect.flatMap(PortScanner.PortDiscovery, (scanner) => scanner.scan()).pipe(
658+
const exit = yield* Effect.flatMap(ownedScanner(), (scanner) => scanner.scan()).pipe(
659659
Effect.provide(layer),
660660
Effect.exit,
661661
);
@@ -760,7 +760,7 @@ effectIt.effect("keeps spawning lsof after a spawn failure that is not a missing
760760
);
761761
});
762762
return Effect.gen(function* () {
763-
const scanner = yield* PortScanner.PortDiscovery;
763+
const scanner = yield* ownedScanner();
764764
yield* scanner.scan();
765765
yield* scanner.scan();
766766
expect(lsofSpawns).toBe(2);
@@ -771,7 +771,7 @@ effectIt.effect("does not swallow process probe interruption", () =>
771771
Effect.gen(function* () {
772772
const layer = layerProbeFailure(() => Effect.interrupt);
773773

774-
const exit = yield* Effect.flatMap(PortScanner.PortDiscovery, (scanner) => scanner.scan()).pipe(
774+
const exit = yield* Effect.flatMap(ownedScanner(), (scanner) => scanner.scan()).pipe(
775775
Effect.provide(layer),
776776
Effect.exit,
777777
);
@@ -783,6 +783,73 @@ effectIt.effect("does not swallow process probe interruption", () =>
783783
}),
784784
);
785785

786+
effectIt.effect.each(["linux", "darwin"] as const)(
787+
"skips idle listener scans on %s and resumes when candidates exist",
788+
(platform) => {
789+
let processCalls = 0;
790+
const layer = PortScanner.layer.pipe(
791+
Layer.provide(
792+
Layer.mergeAll(
793+
layerNoProc,
794+
Layer.succeed(HostProcess.Platform, platform),
795+
Layer.succeed(ProcessRunner.ProcessRunner, {
796+
run: () => {
797+
processCalls += 1;
798+
return Effect.succeed({
799+
stdout: `p1234\ncnode\nn*:${LSOF_TEST_PORT}\n`,
800+
stderr: "",
801+
code: ChildProcessSpawner.ExitCode(0),
802+
timedOut: false,
803+
stdoutTruncated: false,
804+
stderrTruncated: false,
805+
stdoutInvalidUtf8: false,
806+
stderrInvalidUtf8: false,
807+
});
808+
},
809+
}),
810+
FetchHttpClient.layer.pipe(
811+
Layer.provide(
812+
Layer.succeed(FetchHttpClient.Fetch, () =>
813+
Promise.resolve(
814+
new Response("hello", { headers: { "content-type": "text/html" } }),
815+
),
816+
),
817+
),
818+
),
819+
),
820+
),
821+
);
822+
return Effect.gen(function* () {
823+
const scanner = yield* PortScanner.PortDiscovery;
824+
yield* scanner.retain;
825+
yield* TestClock.adjust("9 seconds");
826+
expect(yield* scanner.scan()).toEqual([]);
827+
expect(processCalls).toBe(0);
828+
829+
yield* scanner.registerTerminalProcesses({
830+
threadId: "scanner-thread",
831+
terminalId: "scanner-terminal",
832+
processIds: [1234],
833+
});
834+
expect(yield* scanner.scan()).toMatchObject([{ port: LSOF_TEST_PORT, pid: 1234 }]);
835+
expect(processCalls).toBe(1);
836+
837+
yield* scanner.unregisterTerminal({
838+
threadId: "scanner-thread",
839+
terminalId: "scanner-terminal",
840+
});
841+
yield* TestClock.adjust("9 seconds");
842+
expect(yield* scanner.scan()).toEqual([]);
843+
expect(processCalls).toBe(1);
844+
845+
expect(yield* scanner.scan([`http://localhost:${LSOF_TEST_PORT}/docs`])).toMatchObject([
846+
{ port: LSOF_TEST_PORT, url: `http://localhost:${LSOF_TEST_PORT}/docs` },
847+
]);
848+
expect(processCalls).toBe(2);
849+
}).pipe(Effect.provide(layer));
850+
},
851+
);
852+
786853
effectIt.effect(
787854
"parses Windows netstat listeners, names them via tasklist with retry, and skips spawns when idle",
788855
() => {

‎apps/server/src/preview/PortScanner.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,10 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
692692
terminalByProcessId.set(processId, registration.owner);
693693
}
694694
}
695+
if (terminalByProcessId.size === 0 && configuredUrls.length === 0) {
696+
return { discovered: [], configured: new Map() } satisfies WebProbeSnapshot;
697+
}
695698
if (hostPlatform === "win32") {
696-
if (terminalByProcessId.size === 0 && configuredUrls.length === 0) {
697-
return { discovered: [], configured: new Map() } satisfies WebProbeSnapshot;
698-
}
699699
const recoverWindowsProbeFailure = recoverProcessProbeFailure("windows-listeners");
700700
const runNetstat = (protocol: "tcp" | "tcpv6") =>
701701
processRunner.run({

0 commit comments

Comments
 (0)