Skip to content

@plinthjs/cluster ​

A persistent, high-throughput application server for Plinth. A conventional runtime boots the framework on every request; the persistent server boots the Application once and reuses the warm instance across many requests. After every request (success or failure) the worker flushes scoped container instances, so per-request state never leaks into the next request.

Built only on Node built-ins (node:http, node:cluster) plus @plinthjs/core and @plinthjs/http.

Install ​

sh
npm install @plinthjs/cluster

Usage ​

ts
import { Application } from '@plinthjs/core'
import { HttpKernel, Response, Router } from '@plinthjs/http'
import { ClusterServer, ClusterWorker } from '@plinthjs/cluster'

const app = new Application({ runningInConsole: false })
const router = new Router()
router.get('/ping', () => Response.json({ pong: true }))
const kernel = new HttpKernel(router, { container: app })

const worker = new ClusterWorker({ app, kernel })
const server = new ClusterServer(worker)

await server.start({ port: 8000, host: '0.0.0.0' })

// Later, on shutdown: closes the HTTP server, stops ticks, terminates the app.
await server.stop()

The worker ​

ClusterWorker is the testable core. Give it a ready { app, kernel } context, or a factory that is invoked once on first boot:

ts
const worker = new ClusterWorker({ factory: async () => buildContext() })

await worker.boot() // idempotent; also boots the Application
const response = await worker.handle(request) // runs through the warm HttpKernel
worker.requestsHandled() // 1
worker.isBooted() // true

Lifecycle hooks ​

Every hook registrar returns the worker, so they chain:

ts
worker
  .onBoot((app) => warmCaches(app)) // once, after the first boot
  .onRequest((request) => metrics.start(request))
  .onRequestHandled((request, response) => metrics.finish(request, response))
  .onTick(() => flushBuffers(), 5_000) // every 5 seconds while serving
  .onTerminate((app) => closeConnections(app))

Tick timers are started by server.start() (or worker.startTicking()), are unref'd so they never hold the process open, and swallow errors so a failing task cannot crash the worker. Call worker.tick() to run every tick hook once, which is handy in tests.

Clustering ​

ts
await server.start({ port: 8000, workers: 4 })

With workers > 1 the primary process forks that many workers via node:cluster (re-forking any that exit) and resolves undefined; each worker serves its own warm instance. Under a test runner the server always runs single-process. Use server.build() to get an unbound node:http server for a booted worker, and getServer() / getWorker() to inspect the running instance.

Concurrent tasks ​

ts
import { concurrently } from '@plinthjs/cluster'

const [users, orders] = await concurrently([() => loadUsers(), () => loadOrders()])

Released under the MIT License.