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,
|
||||
|
||||
Reference in new issue
Block a user