Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A single instance of Node.js runs in a single thread.
- To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.
- The cluster module allows easy creation of child processes that all share server ports.
- Fork workers.
- Workers can share any TCP connection
- In this case it is an HTTP server
- Running Node.js will now share port 8000 between the workers:
- Please note that on Windows, it is not yet possible to set up a named pipe server in a worker.
- How It Works
- The worker processes are spawned using the method, so that they can communicate with the parent via IPC and pass server handles back and forth.
- The cluster module supports two methods of distributing incoming connections.
- The first one (and the default one on all platforms except Windows), is the round-robin approach, where the master process listens on a port, accepts new connections and distributes them across the workers in a round-robin fashion, with some built-in smarts to avoid overloading a worker process.
- The second approach is where the master process creates the listen socket and sends it to interested workers. The workers then accept incoming connections directly.
- The second approach should, in theory, give the best performance. In practice however, distribution tends to be very unbalanced due to operating system scheduler vagaries. Loads have been observed where over 70% of all connections ended up in just two processes, out of a total of eight.
- Because hands off most of the work to the master process, there are three cases where the behavior between a normal Node.js process and a cluster worker differs:
- Because the message is passed to the master, file descriptor 7 in the parent will be listened on, and the handle passed to the worker, rather than listening to the worker's idea of what the number 7 file descriptor references.
- Listening on handles explicitly will cause the worker to use the supplied handle, rather than talk to the master process.
- Normally, this will cause servers to listen on a random port. However, in a cluster, each worker will receive the same "random" port each time they do. In essence, the port is random the first time, but predictable thereafter. To listen on a unique port, generate a port number based on the cluster worker ID.
- Node.js does not provide routing logic. It is, therefore important to design an application such that it does not rely too heavily on in-memory data 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. As long as there are some workers still alive, the server will continue to accept connections. If no workers are alive, existing connections will be dropped and new connections will be refused. Node.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.
- Although a primary use case for the cluster module is networking, it can also be used for other use cases requiring worker processes.
- A object contains all public information and method about a worker.
- In the master it can be obtained using
- In a worker it can be obtained using
- Similar to the event, but specific to this worker.
- Worker has disconnected
- This event is the same as the one provided by
- Within a worker, may also be used
- The exit code, if it exited normally.
- The name of the signal (e.g.) that caused the process to be killed.
- It is not emitted in the worker.
- As an example, here is a cluster that keeps count of the number of requests in the master process using the message system:
- Keep track of http requests
- Count requests
- Start workers and listen for messages containing
- Worker processes have a http server.
- notify master about the request
- Worker is online
- Returns: A reference to
- In a worker, this function will close all servers, wait for the event on those servers, and then disconnect the IPC channel.
- In the master, an internal message is sent to the worker causing it to call on itself.
- Causes to be set.
- Note that after a server is closed, it will no longer accept new connections, but connections may be accepted by any other listening worker. Existing connections will be allowed to close as usual. When no more connections exist, see, the IPC channel to the worker will close allowing it to die gracefully.
- The above applies only to server connections, client connections are not automatically closed by workers, and disconnect does not wait for them to close before exiting.
- Note that in a worker, exists, but it is not this function, it is
- Because long living server connections may block workers from disconnecting, it may be useful to send a message, so application specific actions may be taken to close them. It also may be useful to implement a timeout, killing a worker if the event has not been emitted after some time.
- connections never end
- initiate graceful close of any connections to server
- Set by calling or
- Until then, it is
- The boolean allows distinguishing between voluntary and accidental exit, the master may choose not to respawn a worker based on this value.
- kill worker
- Each new worker is given its own unique id, this id is stored in the
- While a worker is alive, this is the key that indexes it in
- This function returns if the worker is connected to its master via its IPC channel, otherwise. A worker is connected to its master after it has been created. It is disconnected after the event is emitted.
- This function returns if the worker's process has terminated (either because of exiting or being signaled). Otherwise, it returns
- Name of the kill signal to send to the worker process.
- This function will kill the worker.
- In the master, it does this by disconnecting the, and once disconnected, killing with
- In the worker, it does it by disconnecting the channel, and then exiting with code
- Causes to be set.
- This method is aliased as for backwards compatibility.
- Note that in a worker, exists, but it is not this function, it is
- All workers are created using, the returned object from this function is stored as
- In a worker, the global is stored.
- Note that workers will call if the event occurs on and is not
- This protects against accidental disconnection.
- Send a message to a worker or master, optionally with a handle.
- In the master this sends a message to a specific worker. It is identical to
- In a worker this sends a message to the master. It is identical to
- This example will echo back all messages from the master:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement