perf(http): resolve ctx.ip on first read instead of on every request
Every Bun request called server.requestIP() for ctx.ip (#116), a native call that builds an object, whether anything read the address or not. On Mac that measured 7-11% of a small request; recording the server in the peer map instead costs nothing. The neutral request now carries a resolver rather than an address. The handler request and the pipeline seed expose ctx.ip as an accessor that resolves once on first read, and a step may still assign it. runPipeline uses the seed it is given instead of spreading it, which would have evaluated the accessor; both callers build the seed fresh per request. Inside a stream body ctx.ip is '' unless a step or the handler read it before the stream was returned: the body runs after the response has started, when the socket may be gone. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
9 files changed
+283
-57
No files matched your search
+3
-2
@@ -127,8 +127,9 @@ export async function serveBun(app: App, options?: BunServeShortOptions): Promis
|
||||
return new Response('WebSocket upgrade failed', { status: 400 });
|
||||
}
|
||||
|
||||
// The same object goes to `app.fetch`, which is what lets it find the peer again.
|
||||
setPeer(request, server.requestIP(request)?.address ?? '');
|
||||
// The same object goes to `app.fetch`, which is what lets it find the peer again. The server,
|
||||
// not its answer: `requestIP` runs only if something reads `ctx.ip` (see `peer.ts`).
|
||||
setPeer(request, server);
|
||||
return app.fetch(request);
|
||||
},
|
||||
websocket: {
|
||||
|
||||
Reference in new issue
Block a user