Skip to content

Commit

Permalink
[WIP] Concurrent request debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Oct 10, 2023
1 parent 7ec7fa3 commit b7b2bf1
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
import exitHook from "exit-hook";
import { $ as colors$ } from "kleur/colors";
import stoppable from "stoppable";
import { Client } from "undici";
import { Dispatcher, Pool } from "undici";
import SCRIPT_MINIFLARE_SHARED from "worker:shared/index";
import SCRIPT_MINIFLARE_ZOD from "worker:shared/zod";
import { WebSocketServer } from "ws";
Expand Down Expand Up @@ -556,7 +556,7 @@ export class Miniflare {
readonly #removeRuntimeExitHook?: () => void;
#runtimeEntryURL?: URL;
#socketPorts?: SocketPorts;
#runtimeClient?: Client;
#runtimeDispatcher?: Dispatcher;
#proxyClient?: ProxyClient;

// Path to temporary directory for use as scratch space/"in-memory" Durable
Expand Down Expand Up @@ -1136,10 +1136,10 @@ export class Miniflare {
`${secure ? "https" : "http"}://${accessibleHost}:${entryPort}`
);
if (previousEntryURL?.toString() !== this.#runtimeEntryURL.toString()) {
this.#runtimeClient = new Client(this.#runtimeEntryURL, {
this.#runtimeDispatcher = new Pool(this.#runtimeEntryURL, {
connect: { rejectUnauthorized: false },
});
registerAllowUnauthorizedDispatcher(this.#runtimeClient);
registerAllowUnauthorizedDispatcher(this.#runtimeDispatcher);
}
if (this.#proxyClient === undefined) {
this.#proxyClient = new ProxyClient(
Expand Down Expand Up @@ -1290,7 +1290,7 @@ export class Miniflare {
await this.ready;

assert(this.#runtimeEntryURL !== undefined);
assert(this.#runtimeClient !== undefined);
assert(this.#runtimeDispatcher !== undefined);

const forward = new Request(input, init);
const url = new URL(forward.url);
Expand All @@ -1312,9 +1312,16 @@ export class Miniflare {
}

const forwardInit = forward as RequestInit;
forwardInit.dispatcher = this.#runtimeClient;
forwardInit.dispatcher = this.#runtimeDispatcher;

const response = await fetch(url, forwardInit);

const symbols = Object.getOwnPropertySymbols(this.#runtimeDispatcher);
const kClients = symbols.find((symbol) => symbol.description === "clients");
assert(kClients !== undefined);
const clients = (this.#runtimeDispatcher as any)[kClients];
console.warn({ clientsLength: clients.length });

// If the Worker threw an uncaught exception, propagate it to the caller
const stack = response.headers.get(CoreHeaders.ERROR_STACK);
if (response.status === 500 && stack !== null) {
Expand Down

0 comments on commit b7b2bf1

Please sign in to comment.