fix(mesh): close teacup links on shutdown/boot-failure; match proxy routes by method

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-06-27 20:42:44 -05:00
1 parent e8a63e8d41
commit e702de187d
1 file changed
+10 -2
+10 -2
View File
@@ -11,7 +11,7 @@ import {
Ctor, HttpMethod, Transport, getModuleMeta, getProviderMeta, getStepMeta, getRoutes, getTransformer, joinPath, Ctor, HttpMethod, Transport, getModuleMeta, getProviderMeta, getStepMeta, getRoutes, getTransformer, joinPath,
} from './metadata'; } from './metadata';
import { getArgs, getHandlerNeeds, resolveArgs } from './params'; import { getArgs, getHandlerNeeds, resolveArgs } from './params';
import { connectLink } from './mesh/link'; import { connectLink, type Link } from './mesh/link';
import { buildRemote } from './mesh/teacup'; import { buildRemote } from './mesh/teacup';
import { buildManifest, createMeshControl } from './mesh/teapot'; import { buildManifest, createMeshControl } from './mesh/teapot';
import type { MeshControl } from './http'; import type { MeshControl } from './http';
@@ -171,10 +171,13 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
const listen = async (port: number): Promise<http.Server> => { const listen = async (port: number): Promise<http.Server> => {
// mesh teacup: connect remote teapots, splice their scopes/routes into the graph, then finalize // mesh teacup: connect remote teapots, splice their scopes/routes into the graph, then finalize
const remoteRoutes: RouteDef[] = []; const remoteRoutes: RouteDef[] = [];
const meshLinks: Link[] = [];
let meshControl: MeshControl | undefined; let meshControl: MeshControl | undefined;
if (opts.mesh && !booted) { if (opts.mesh && !booted) {
try {
for (const t of opts.mesh.teapots ?? []) { for (const t of opts.mesh.teapots ?? []) {
const link = await connectLink({ url: t.url, secret: t.secret, timeoutMs: opts.mesh.timeoutMs, bus }); const link = await connectLink({ url: t.url, secret: t.secret, timeoutMs: opts.mesh.timeoutMs, bus });
meshLinks.push(link);
const { providers, steps, routes } = buildRemote(link, t.url); const { providers, steps, routes } = buildRemote(link, t.url);
for (const p of providers) { for (const p of providers) {
providerNodes.push({ name: p.name, needs: [], provides: [p.name], origin: `mesh:${t.url}` }); providerNodes.push({ name: p.name, needs: [], provides: [p.name], origin: `mesh:${t.url}` });
@@ -193,6 +196,10 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
} }
} }
finalizeGraph(); finalizeGraph();
} catch (err) {
for (const l of meshLinks) { try { l.close(); } catch { /* */ } }
throw err;
}
} }
// run app-scoped providers in order (fail-fast for required) // run app-scoped providers in order (fail-fast for required)
@@ -228,7 +235,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
return ctx[name]; return ctx[name];
}; };
const resolveRoute = async (name: string, env: RequestEnvelope) => { const resolveRoute = async (name: string, env: RequestEnvelope) => {
const plan = routePlans.find((p) => p.pattern === name); const plan = routePlans.find((p) => p.pattern === name && p.method === env.method);
if (!plan) { const e: any = new Error(`no route ${name}`); e.status = 404; throw e; } if (!plan) { const e: any = new Error(`no route ${name}`); e.status = 404; throw e; }
const provided = await providedSeed(plan); const provided = await providedSeed(plan);
const result = await runPipeline({ const result = await runPipeline({
@@ -273,6 +280,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
})); }));
const server = createHttpServer(httpRoutes, wsRoutes, bus, meshControl); const server = createHttpServer(httpRoutes, wsRoutes, bus, meshControl);
server.on('close', () => { for (const l of meshLinks) { try { l.close(); } catch { /* */ } } });
await new Promise<void>((resolve) => server.listen(port, resolve)); await new Promise<void>((resolve) => server.listen(port, resolve));
return server; return server;
}; };