diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ec1fd4..d2389a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ npm treats versions as semver and semver forbids leading zeros. ## [Unreleased] +### Changed + +- **Mesh: teapots connect in parallel at boot** (#95). Each absent teapot used to cost its own + `bootTimeoutMs` before the next one was tried, so three absent teapots held the boot for three + budgets; now they cost one. Remote steps and routes still register in config order, so the graph + does not depend on which teapot answers first. A refusal (wrong secret) still fails the boot at + once, without waiting for an absent teapot's budget. Mesh is `@experimental`; no API change. + ## [26.11.0-rc.2] - 2026-11-23 ### Added diff --git a/src/app/index.ts b/src/app/index.ts index 9c1eb73..3ad2b63 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -1528,10 +1528,33 @@ async function spliceRemoteScopes( ); const ctx: MembershipContext = { mesh, bus, logger, table }; + // All teapots connect at once, so N absent ones cost one boot budget rather than N (#95). A refusal + // still fails the boot immediately; the attempts still running are closed as they land, since + // nothing else will ever hold their links. + const holders = teapots.map((): { link?: Link } => ({})); + const attempts = teapots.map((teapot, i) => + connectUntilDeadline(mesh, bus, logger, teapot.url, () => connectTeapot(ctx, teapot, holders[i]!)), + ); + let links: (Link | undefined)[]; + + try { + links = await Promise.all(attempts); + } catch (err) { + for (const attempt of attempts) + attempt.then( + (link) => link?.close(), + () => {}, + ); + throw err; + } + + // Registration walks the config order, not arrival order, so the graph is the same on every boot. + meshLinks.push(...links.filter((link): link is Link => link !== undefined)); + try { - for (const teapot of teapots) { - const holder: { link?: Link } = {}; - const link = await connectUntilDeadline(mesh, bus, logger, teapot.url, () => connectTeapot(ctx, teapot, holder)); + for (const [i, teapot] of teapots.entries()) { + const link = links[i]; + const holder = holders[i]!; if (!link) { absent.push(teapot.url); @@ -1541,7 +1564,6 @@ async function spliceRemoteScopes( } holder.link = link; - meshLinks.push(link); const { steps, routes } = buildRemote(link); const origin = `mesh:${teapot.url}`; diff --git a/test/bun/mesh-reconnect.test.ts b/test/bun/mesh-reconnect.test.ts index 3cada4c..47af3f7 100644 --- a/test/bun/mesh-reconnect.test.ts +++ b/test/bun/mesh-reconnect.test.ts @@ -187,3 +187,29 @@ test('mesh on Bun: a reconnecting teapot keeps its routes with 503 + retry-after await teacup.close(); } }); + +// #95: absent teapots connect in parallel, so three of them cost one boot budget, not three. +@Module({ mountpoint: '/api', controllers: [] }) +class EmptyModule {} + +test('mesh on Bun: three absent teapots boot in about one bootTimeoutMs', async () => { + const teacup = createApp({ + modules: [EmptyModule], + experimental: true, + logger: { debug() {}, info() {}, warn() {}, error() {} }, + mesh: { + teapots: [9, 10, 11].map((port) => ({ url: `ws://127.0.0.1:${port}/x`, secret: SECRET })), + secret: SECRET, + bootTimeoutMs: 600, + }, + }); + + try { + const started = Date.now(); + await teacup.ready(); + const elapsed = Date.now() - started; + expect(elapsed).toBeLessThan(1500); + } finally { + await teacup.close(); + } +}, 15_000); diff --git a/test/deno/mesh-reconnect.test.ts b/test/deno/mesh-reconnect.test.ts index 819b414..38b029b 100644 --- a/test/deno/mesh-reconnect.test.ts +++ b/test/deno/mesh-reconnect.test.ts @@ -192,3 +192,29 @@ Deno.test('mesh on Deno: a reconnecting teapot keeps its routes with 503 + retry await teacup.close(); } }); + +// #95: absent teapots connect in parallel, so three of them cost one boot budget, not three. +@Module({ mountpoint: '/api', controllers: [] }) +class EmptyModule {} + +Deno.test('mesh on Deno: three absent teapots boot in about one bootTimeoutMs', async () => { + const teacup = createApp({ + modules: [EmptyModule], + experimental: true, + logger: { debug() {}, info() {}, warn() {}, error() {} }, + mesh: { + teapots: [9, 10, 11].map((port) => ({ url: `ws://127.0.0.1:${port}/x`, secret: SECRET })), + secret: SECRET, + bootTimeoutMs: 600, + }, + }); + + try { + const started = Date.now(); + await teacup.ready(); + const elapsed = Date.now() - started; + assertEquals(elapsed < 1500, true, `took ${elapsed}ms`); + } finally { + await teacup.close(); + } +}); diff --git a/test/mesh/boot-retry.test.ts b/test/mesh/boot-retry.test.ts index 6100942..f686728 100644 --- a/test/mesh/boot-retry.test.ts +++ b/test/mesh/boot-retry.test.ts @@ -310,4 +310,57 @@ describe('mesh boot retry', () => { await teacup.close(); } }, 15_000); + + it('connects absent teapots in parallel, so three of them cost one boot budget (#95)', async () => { + // Sequential connects spent bootTimeoutMs per absent teapot: 3 × 600ms here, ~1.8s. + const teacup = createApp({ + modules: [LocalOnlyModule], + experimental: true, + logger: collect().logger, + mesh: { + teapots: [9, 10, 11].map((port) => ({ url: `ws://127.0.0.1:${port}/x`, secret: 's' })), + secret: 's', + bootTimeoutMs: 600, + }, + }); + + try { + const started = Date.now(); + await teacup.ready(); + expect(Date.now() - started).toBeLessThan(1500); + } finally { + await teacup.close(); + } + }, 15_000); + + it('still fails fast on a refusal while another teapot is absent', async () => { + // In parallel the refusal no longer waits for the absent teapot's budget to run out first. + const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: 'good' } }); + const server = await teapot.listen(0); + const port = (server.address() as { port: number }).port; + + const teacup = createApp({ + modules: [TeacupModule], + experimental: true, + logger: collect().logger, + mesh: { + teapots: [ + { url: 'ws://127.0.0.1:9/x', secret: 's' }, + { url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: 'WRONG' }, + ], + timeoutMs: 500, + bootTimeoutMs: 30_000, + }, + }); + + try { + const started = Date.now(); + await expect(teacup.ready()).rejects.toThrow(/mesh/); + expect(Date.now() - started).toBeLessThan(3000); + } finally { + await teacup.close(); + await teapot.close(); + server.close(); + } + }, 40_000); });