perf(router): index HTTP routes in a radix tree
Replace the linear, specificity-sorted scan behind matchRoute, resolveRoute and allowedMethods with a segment trie per HTTP method, built once per route array from the compiled segments compilePattern already produces. Ranking stays global: every terminal carries its position in the old sorted bucket (moreSpecific, declaration order on ties), and the walk keeps the lowest rank it can reach, pruning subtrees that cannot beat it. The tree decides which routes can match; the rank alone decides which one does. A static branch that dead-ends deeper backtracks into its param, constrained and catch-all siblings. All-static routes are also kept by exact path per method. A hit there is provably the tree's best match, so a static request is answered without splitting the path; a miss falls through to the walk. Mesh-spliced remote routes go in a second tree, per the owner's design decision. Both trees answer, the more specific match wins and an equal one goes to the earlier declaration, so local still beats remote on a tie. Allow, 405 and implicit HEAD run over the union. An app without remote routes does one lookup. allowedMethods walks the index instead of recompiling every route on a miss, and an implicit HEAD no longer builds a fresh index from a filtered array on every request. The frozen parity corpus passes unchanged. The frozen mutant script cannot run against the new layout by design, so test/router-index-mutants.ts re-targets its twelve bug classes at the new code and runs the same untouched suite. Refs #109 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+731
-72
No files matched your search
+22
-19
@@ -49,7 +49,7 @@ import type { MeshControl } from '../http';
|
||||
import type { RequestEnvelope, RouteEntry } from '../mesh/protocol';
|
||||
import type { App, InspectLine, Explain, RoutePlan, MeshConfig } from './types';
|
||||
import { inspectRoute, buildGraphView, explainRoute } from './introspect';
|
||||
import { compilePattern, normalizeRequestPath } from '../http/router';
|
||||
import { compilePattern, markRemoteRoute, normalizeRequestPath } from '../http/router';
|
||||
|
||||
export type { App, InspectLine, ExplainNode, Explain, MeshConfig, Hooks } from './types';
|
||||
|
||||
@@ -952,24 +952,27 @@ async function spliceRemoteScopes(
|
||||
);
|
||||
}
|
||||
|
||||
remoteRoutes.push({
|
||||
method: route.method,
|
||||
pattern: route.pattern,
|
||||
transport: 'buffer',
|
||||
// Built explicitly rather than cast from the handler argument. The cast put the whole
|
||||
// internal request on the wire — `ip` and `protocol` included — and sent fields the
|
||||
// protocol never declared, so a teapot could come to rely on something we never promised.
|
||||
handler: async (req) =>
|
||||
route.handler({
|
||||
method: req.method,
|
||||
params: req.params,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
headers: req.headers,
|
||||
url: req.url,
|
||||
correlation: { requestId: req.requestId, traceId: req.traceId },
|
||||
}),
|
||||
});
|
||||
// Marked so the router files it in the remote tree; precedence is unchanged by the split.
|
||||
remoteRoutes.push(
|
||||
markRemoteRoute({
|
||||
method: route.method,
|
||||
pattern: route.pattern,
|
||||
transport: 'buffer',
|
||||
// Built explicitly rather than cast from the handler argument. The cast put the whole
|
||||
// internal request on the wire — `ip` and `protocol` included — and sent fields the
|
||||
// protocol never declared, so a teapot could come to rely on something we never promised.
|
||||
handler: async (req) =>
|
||||
route.handler({
|
||||
method: req.method,
|
||||
params: req.params,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
headers: req.headers,
|
||||
url: req.url,
|
||||
correlation: { requestId: req.requestId, traceId: req.traceId },
|
||||
}),
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
Reference in new issue
Block a user