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
+18
-6
@@ -12,18 +12,30 @@
|
||||
// with no shared chunk, so each carries its own copy of this one: a module-private map in
|
||||
// `@green-tea/core/bun` would record peers that `app.fetch` from `@green-tea/core` never sees. An app
|
||||
// that loads core both as ESM and as CommonJS has the same split.
|
||||
//
|
||||
// Bun records its server rather than an address. `server.requestIP()` is a native call that builds
|
||||
// an object, and making it on every request measured 7–11% on Bun; recording the server is free, and
|
||||
// the address is asked for only when something reads `ctx.ip`. Deno hands the address over already
|
||||
// resolved, so it records the string.
|
||||
const KEY = Symbol.for('@green-tea/core:peer');
|
||||
const registry = globalThis as { [KEY]?: WeakMap<Request, string> };
|
||||
const peers = (registry[KEY] ??= new WeakMap<Request, string>());
|
||||
|
||||
/** Records `ip` as the peer of `request`. An empty address — a unix socket has none — is not recorded. */
|
||||
export function setPeer(request: Request, ip: string): void {
|
||||
if (ip) peers.set(request, ip);
|
||||
/** Names a request's socket peer on demand: Bun's server. */
|
||||
export interface PeerSource {
|
||||
requestIP(request: Request): { address: string } | null;
|
||||
}
|
||||
|
||||
const registry = globalThis as { [KEY]?: WeakMap<Request, string | PeerSource> };
|
||||
const peers = (registry[KEY] ??= new WeakMap<Request, string | PeerSource>());
|
||||
|
||||
/** Records the peer of `request`. An empty address — a unix socket has none — is not recorded. */
|
||||
export function setPeer(request: Request, peer: string | PeerSource): void {
|
||||
if (peer) peers.set(request, peer);
|
||||
}
|
||||
|
||||
/** The peer an adapter recorded for `request`, or `''` when none did. */
|
||||
export function peerOf(request: Request): string {
|
||||
return peers.get(request) ?? '';
|
||||
const peer = peers.get(request);
|
||||
return typeof peer === 'object' ? (peer.requestIP(request)?.address ?? '') : (peer ?? '');
|
||||
}
|
||||
|
||||
// The client address a runtime adapter read from a header its platform's proxy sets — today only the
|
||||
|
||||
Reference in new issue
Block a user