Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d850639

Browse files
committed
Use http instead of undici when waiting for runtime
...and disable keep-alives. This was a debugging step, but is probably a good thing to do anyway. We're making lots of repeated requests here, `http` is probably more optimised than `undici` atm. In most cases, the socket will be closed as `workerd` won't be listening yet, so keep-alives don't make sense.
1 parent ca3272d commit d850639

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

packages/tre/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,10 @@ export class Miniflare {
620620
exitController.signal,
621621
this.#disposeController.signal
622622
);
623-
await waitForRequest(this.#runtimeEntryURL!, {
623+
const url = this.#runtimeEntryURL!;
624+
await waitForRequest({
625+
hostname: url.hostname,
626+
port: url.port,
624627
headers: { [HEADER_PROBE]: this.#optionsVersion.toString() },
625628
signal,
626629
});

packages/tre/src/wait.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import http from "http";
12
import type { TimerOptions } from "timers";
23
import { setTimeout } from "timers/promises";
3-
import { request } from "undici";
44

55
function attemptDelay(attempts: number) {
66
if (attempts < 10) return 10;
@@ -9,28 +9,40 @@ function attemptDelay(attempts: number) {
99
return 1000;
1010
}
1111

12-
export async function waitForRequest(...options: Parameters<typeof request>) {
12+
// Disable keep-alive for polling requests
13+
const agent = new http.Agent({ keepAlive: false, maxSockets: 1 });
14+
15+
function request(options: http.RequestOptions) {
16+
return new Promise<number>((resolve, reject) => {
17+
const req = http.request(options, (res) => {
18+
resolve(res.statusCode ?? 0);
19+
res.destroy();
20+
});
21+
req.on("error", (err) => reject(err));
22+
req.end();
23+
});
24+
}
25+
26+
export async function waitForRequest(options: http.RequestOptions) {
27+
options = { ...options, agent };
28+
1329
let attempts = 0;
14-
const signal = options[1]?.signal as AbortSignal | undefined;
30+
const signal = options.signal;
1531
const timeoutOptions: TimerOptions = { signal };
1632

1733
while (!signal?.aborted) {
1834
try {
19-
const res = await request(...options);
20-
const code = res.statusCode;
35+
const code = await request(options);
2136
if (code !== undefined && 200 <= code && code < 300) return;
2237
} catch (e: any) {
2338
const code = e.code;
24-
if (code === "UND_ERR_ABORTED") return;
39+
if (code === "ABORT_ERR") return;
2540
if (
2641
// Adapted from https://github.com/dwmkerr/wait-port/blob/0d58d29a6d6b8ea996de9c6829706bb3b0952ee8/lib/wait-port.js
2742
code !== "ECONNREFUSED" &&
2843
code !== "ECONNTIMEOUT" &&
2944
code !== "ECONNRESET" &&
30-
code !== "ENOTFOUND" &&
31-
// Docker published port, but not bound in container
32-
code !== "ECONNRESET" &&
33-
code !== "UND_ERR_SOCKET"
45+
code !== "ENOTFOUND"
3446
) {
3547
throw e;
3648
}

0 commit comments

Comments
 (0)