Table of Contents
A single example of Knot.js runs ter a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Knot.js processes to treat the fountain.
The cluster module permits effortless creation of child processes that all share server ports.
Running Knot.js will now share port 8000 inbetween the workers:
Please note that on Windows, it is not yet possible to set up a named pipe server te a worker.
How It Works #
The worker processes are spawned using the child_process.fork() method, so that they can communicate with the parent via IPC and pass server treats back and forward.
The cluster module supports two methods of distributing incoming connections.
The very first one (and the default one on all platforms except Windows), is the round-robin treatment, where the master process listens on a port, accepts fresh connections and distributes them across the workers te a round-robin style, with some built-in smarts to avoid overloading a worker process.
The 2nd treatment is where the master process creates the listen socket and sends it to interested workers. The workers then accept incoming connections directly.
The 2nd treatment should, te theory, give the best spectacle. Te practice however, distribution tends to be very unbalanced due to operating system scheduler vagaries. Geysers have bot observed where overheen 70% of all connections ended up ter just two processes, out of a total of eight.
Because server.listen() mitts off most of the work to the master process, there are three cases where the behavior inbetween a normal Knot.js process and a cluster worker differs:
- server.listen() Because the message is passed to the master, opstopping descriptor 7 te the parent will be listened on, and the treat passed to the worker, rather than listening to the worker’s idea of what the number 7 verkeersopstopping descriptor references.
- server.listen(treat) Listening on treats explicitly will cause the worker to use the supplied treat, rather than talk to the master process.
- server.listen(0) Normally, this will cause servers to listen on a random port. However, te a cluster, each worker will receive the same ",random", port each time they do listen(0) . Te essence, the port is random the very first time, but predictable thereafter. To listen on a unique port, generate a port number based on the cluster worker ID.
Knot.js does not provide routing logic. It is, therefore significant to vormgeving an application such that it does not rely too strenuously on in-memory gegevens objects for things like sessions and login.
Because workers are all separate processes, they can be killed or re-spawned depending on a program’s needs, without affecting other workers. Spil long spil there are some workers still alive, the server will proceed to accept connections. If no workers are alive, existing connections will be dropped and fresh connections will be refused. Knot.js does not automatically manage the number of workers, however. It is the application’s responsibility to manage the worker pool based on its own needs.
Albeit a primary use case for the cluster module is networking, it can also be used for other use cases requiring worker processes.
Class: Worker #
A Worker object contains all public information and method about a worker. Te the master it can be obtained using cluster.workers . Ter a worker it can be obtained using cluster.worker .
Event: ‘disconnect’ #
Similar to the cluster.on(‘disconnect’) event, but specific to this worker.
Event: ‘error’ #
This event is the same spil the one provided by child_process.fork() .
Within a worker, process.on(‘error’) may also be used.
Event: ‘uitgang’ #
- code <,number>, The uitgang code, if it exited normally.
- signal <,string>, The name of the signal (e.g. ‘SIGHUP’ ) that caused the process to be killed.
Similar to the cluster.on(‘uitgang’) event, but specific to this worker.
Event: ‘listening’ #
- address <,Object>,
Similar to the cluster.on(‘listening’) event, but specific to this worker.
It is not emitted te the worker.
Event: ‘message’ #
- message <,Object>,
- treat <,undefined>, | <,Object>,
Similar to the cluster.on(‘message’) event, but specific to this worker.
Within a worker, process.on(‘message’) may also be used.
Spil an example, here is a cluster that keeps count of the number of requests te the master process using the message system: