fix(mesh): fail the boot when two teapots export the same route
Duplicate scope tokens already threw in setRunner, but duplicate routes did not: both were pushed into remoteRoutes, and matchRoute keeps the first of two identical patterns (moreSpecific returns 0, not > 0). The second teapot's route was silently dead. Same collision, two behaviours — one loud, one mute. Fail the boot instead, naming the route and both teapots, since which pair clashed is the thing an operator needs. The message says load balancing is not implemented *yet*: it is a not-yet, not an impossibility. Throwing now also keeps the door open. "First teapot wins" is a semantic callers could come to rely on, so shipping it would make balancing a breaking change later. An error cannot be relied on — turning it into a strategy stays additive. Scopes keep failing rather than balancing, and should: an app-scope provider is memoised, so "balancing" a cached value means nothing. Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+84
No files matched your search
@@ -466,6 +466,7 @@ async function spliceRemoteScopes(
|
||||
): Promise<{ remoteRoutes: RouteDef[]; meshLinks: Link[] }> {
|
||||
const remoteRoutes: RouteDef[] = [];
|
||||
const meshLinks: Link[] = [];
|
||||
const routeOwners = new Map<string, string>(); // "METHOD /pattern" -> the teapot that exported it
|
||||
|
||||
try {
|
||||
for (const teapot of mesh.teapots ?? []) {
|
||||
@@ -486,6 +487,22 @@ async function spliceRemoteScopes(
|
||||
}
|
||||
|
||||
for (const route of routes) {
|
||||
const key = `${route.method} ${route.pattern}`;
|
||||
const owner = routeOwners.get(key);
|
||||
|
||||
// Fail the boot rather than pick one. Route matching keeps the first of two identical
|
||||
// patterns, so the loser would be silently dead — and callers could come to depend on
|
||||
// "first teapot wins", which would make adding balancing later a breaking change.
|
||||
// Scope tokens already fail this way in setRunner; routes now match.
|
||||
if (owner) {
|
||||
throw new Error(
|
||||
`mesh: route '${key}' is exported by more than one teapot (${owner} and ${teapot.url}) — ` +
|
||||
'load balancing across teapots is not implemented yet, so green-tea will not choose one for you. ' +
|
||||
'Export it from a single teapot.',
|
||||
);
|
||||
}
|
||||
|
||||
routeOwners.set(key, teapot.url);
|
||||
remoteRoutes.push({
|
||||
method: route.method,
|
||||
pattern: route.pattern,
|
||||
|
||||
@@ -23,6 +23,28 @@ class LocalCtl {
|
||||
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
|
||||
class TeacupModule {}
|
||||
|
||||
// Two teapots exporting the SAME route and no scopes — a scope clash would throw first
|
||||
// (setRunner enforces unique tokens), which would hide the route clash under test.
|
||||
@Route('/dup')
|
||||
class DupCtl {
|
||||
@Get('/ping', { export: true })
|
||||
ping() {
|
||||
return { pong: true };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/api', controllers: [DupCtl] })
|
||||
class DupTeapotModule {}
|
||||
|
||||
@Route('/c')
|
||||
class PlainCtl {
|
||||
@Get('/ok')
|
||||
ok() {
|
||||
return { ok: true };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [PlainCtl] })
|
||||
class PlainTeacupModule {}
|
||||
|
||||
describe('mesh skeleton integration', () => {
|
||||
it('teacup resolves remote app-scope + request-scope into a local handler', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
@@ -52,6 +74,51 @@ describe('mesh skeleton integration', () => {
|
||||
s.close();
|
||||
});
|
||||
|
||||
it('refuses to boot when two teapots export the same route, naming both', async () => {
|
||||
// No load balancing exists yet, so picking one silently would be a wrong answer that
|
||||
// callers could come to depend on — and it would make adding LB later a breaking change.
|
||||
const a = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const b = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const aServer = await a.listen(0);
|
||||
const bServer = await b.listen(0);
|
||||
const url = (s: any) => `ws://127.0.0.1:${(s.address() as any).port}/__mesh__/control`;
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [PlainTeacupModule],
|
||||
experimental: true,
|
||||
mesh: {
|
||||
teapots: [
|
||||
{ url: url(aServer), secret: SECRET },
|
||||
{ url: url(bServer), secret: SECRET },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
await expect(teacup.fetch(new Request('http://x/c/ok'))).rejects.toThrow(/GET \/api\/dup\/ping/);
|
||||
await expect(teacup.fetch(new Request('http://x/c/ok'))).rejects.toThrow(/load balancing/i);
|
||||
|
||||
aServer.close();
|
||||
bServer.close();
|
||||
});
|
||||
|
||||
it('accepts the same route from a single teapot (regression: no false positive)', async () => {
|
||||
const a = createApp({ modules: [DupTeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const aServer = await a.listen(0);
|
||||
const url = `ws://127.0.0.1:${(aServer.address() as any).port}/__mesh__/control`;
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [PlainTeacupModule],
|
||||
experimental: true,
|
||||
mesh: { teapots: [{ url, secret: SECRET }] },
|
||||
});
|
||||
|
||||
const res = await teacup.fetch(new Request('http://x/api/dup/ping'));
|
||||
expect(await res.json()).toEqual({ pong: true });
|
||||
|
||||
await teacup.close();
|
||||
aServer.close();
|
||||
});
|
||||
|
||||
it('rejects a bad secret (teacup boot fails)', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||
const tServer = await teapot.listen(0); const tPort = (tServer.address() as any).port;
|
||||
|
||||
Reference in new issue
Block a user