perf(app): skip awaiting the boot in app.fetch once it has finished
trackHealth wraps the memoized boot in an async function, so every app.fetch request paid a new promise and microtask hops awaiting a boot that had already settled. A built route table means the boot succeeded, so the fetch handler now goes straight to it; only requests before the table exists await the boot. A failed boot still rejects every request, because the table is never built. Measured in-process on Deno (app.fetch in a loop, param route): about +3-6%. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+15
-7
No files matched your search
+10
-7
@@ -1810,6 +1810,15 @@ function buildAppFetch(
|
||||
): (request: Request) => Promise<Response> {
|
||||
let handler: ((request: Request) => Promise<Response>) | undefined;
|
||||
|
||||
// Awaits the boot, then builds the table. Only until the first request has done so: a built table
|
||||
// means the boot succeeded, and awaiting a settled boot again costs every request a promise and
|
||||
// microtask hops (about 6% of a small request on Deno).
|
||||
const firstRequest = async (request: Request): Promise<Response> => {
|
||||
await ensureBooted(); // idempotent: no-op once any of fetch/upgrade/listen has booted
|
||||
handler ??= buildFetch(getRoutes, fetchOpts);
|
||||
return handler(request);
|
||||
};
|
||||
|
||||
return async (request: Request): Promise<Response> => {
|
||||
// Before the boot, or `/__ready__` could never say `booting`: it would wait for the boot to end.
|
||||
// The boot is still started, not awaited: on the edge, or under a bare `Deno.serve(app.fetch)`,
|
||||
@@ -1822,13 +1831,7 @@ function buildAppFetch(
|
||||
return probe;
|
||||
}
|
||||
|
||||
await ensureBooted(); // idempotent: no-op once any of fetch/upgrade/listen has booted
|
||||
|
||||
if (!handler) {
|
||||
handler = buildFetch(getRoutes, fetchOpts);
|
||||
}
|
||||
|
||||
return handler(request);
|
||||
return handler ? handler(request) : firstRequest(request);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user