Expand description
Node cluster — real process-fork model over std::process::Command.
§What is real
cluster.fork([env])spawns a genuine OS child process. It re-launches the SAME runtime binary (std::env::current_exe()) on the SAME entry script (process.argv[1], i.e.settings.exec), with an env marker (CLUSTER_WORKER=<id>+ Node’s ownNODE_UNIQUE_ID=<id>) plus any callerenvoverrides. The child therefore runs the whole program again, but this time in worker mode. This is the actual Unix master/worker fork model, not a simulation.cluster.isPrimary/isMaster/isWorkerare derived from that env marker: the primary has neitherCLUSTER_WORKERnorNODE_UNIQUE_IDset; a forked child has one set, so it reportsisWorker === true.- Worker lifecycle events are real, best-effort:
'fork'fires synchronously fromfork(),'online'is posted onto the event loop immediately after the child launches, and'exit'fires when a background reaper thread observes the child process actually exit (viaChild::wait). Each live workerincr_handles the loop so the primary stays alive while workers run, and'exit'decr_handles it. Worker.kill([signal])delivers a real signal to the child pid (libc::kill), socluster.workers[id].kill()truly terminates the process.cluster.workersmaps live worker id →Worker, andcluster.workerin a forked child is aWorkerfor itself (id from the env marker).
§Documented limitations (honest, never a silent fake)
- No primary↔worker IPC channel. Node connects each fork over a pipe and
ships
worker.send(msg)/process.on('message')across it. node-js does not wire a cross-process pipe here, soWorker.send()is a documented no-op that returnsfalse, and there is no'message'delivery between primary and cluster workers. (In-process message passing exists inworker_threads, which shares one address space; cluster workers are separate OS processes and would need a real socket/pipe channel.) - No shared listening socket. Node’s primary opens the listen socket once
and hands the same file descriptor to every worker so N workers accept on ONE
port (SO_REUSEPORT / fd passing). node-js does not pass fds across the fork,
so each worker that calls
server.listen(port)binds its OWN socket — true round-robin load balancing across workers on a single port is NOT provided. Consequently'listening'is not emitted (no fd hand-off to observe) andWorker.disconnect()cannot gracefully drain an IPC/socket channel: it marks the worker disconnected and emits'disconnect', but the real way to stop a worker isWorker.kill().
Constants§
- METHODS
- Callable module members. The EventEmitter surface is included so that
cluster.on('exit', …)/cluster.emit(…)route throughstdlib::call(cluster.<method>) to the process-wide cluster emitter. - WORKER_
METHODS - Instance methods on a
Worker(@@nativetag"ClusterWorker"), beyond the shared EventEmitter surface.
Functions§
- call
- constant
- Non-function members of the
clusternamespace. - instance_
call