@@ -6,8 +6,8 @@ A single instance of Node.js runs in a single thread. To take advantage of
6
6
multi-core systems the user will sometimes want to launch a cluster of Node.js
7
7
processes to handle the load.
8
8
9
- The cluster module allows you to easily create child processes that
10
- all share server ports.
9
+ The cluster module allows easy creation of child processes that all share
10
+ server ports.
11
11
12
12
``` js
13
13
const cluster = require (' cluster' );
@@ -88,27 +88,24 @@ Node.js process and a cluster worker differs:
88
88
idea of what the number 7 file descriptor references.
89
89
2 . ` server.listen(handle) ` Listening on handles explicitly will cause
90
90
the worker to use the supplied handle, rather than talk to the master
91
- process. If the worker already has the handle, then it's presumed
92
- that you know what you are doing.
91
+ process.
93
92
3 . ` server.listen(0) ` Normally, this will cause servers to listen on a
94
93
random port. However, in a cluster, each worker will receive the
95
94
same "random" port each time they do ` listen(0) ` . In essence, the
96
- port is random the first time, but predictable thereafter. If you
97
- want to listen on a unique port, generate a port number based on the
98
- cluster worker ID.
95
+ port is random the first time, but predictable thereafter. To listen
96
+ on a unique port, generate a port number based on the cluster worker ID.
99
97
100
- There is no routing logic in Node.js, or in your program, and no shared
101
- state between the workers. Therefore, it is important to design your
102
- program such that it does not rely too heavily on in-memory data objects
103
- for things like sessions and login.
98
+ * Note* : Node.js does not provide routing logic. It is, therefore important to
99
+ design an application such that it does not rely too heavily on in-memory data
100
+ objects for things like sessions and login.
104
101
105
102
Because workers are all separate processes, they can be killed or
106
- re-spawned depending on your program's needs, without affecting other
103
+ re-spawned depending on a program's needs, without affecting other
107
104
workers. As long as there are some workers still alive, the server will
108
105
continue to accept connections. If no workers are alive, existing connections
109
- will be dropped and new connections will be refused. Node.js does not
110
- automatically manage the number of workers for you , however. It is your
111
- responsibility to manage the worker pool for your application's needs.
106
+ will be dropped and new connections will be refused. Node.js does not
107
+ automatically manage the number of workers, however. It is the application's
108
+ responsibility to manage the worker pool based on its own needs.
112
109
113
110
114
111
@@ -141,7 +138,7 @@ added: v0.7.3
141
138
142
139
This event is the same as the one provided by [ ` child_process.fork() ` ] [ ] .
143
140
144
- In a worker you can also use ` process.on('error') ` .
141
+ Within a worker, ` process.on('error') ` may also be used .
145
142
146
143
### Event: 'exit'
147
144
<!-- YAML
@@ -192,8 +189,9 @@ added: v0.7.0
192
189
* ` message ` {Object}
193
190
* ` handle ` {undefined|Object}
194
191
195
- Similar to the ` cluster.on('message') ` event, but specific to this worker. In a
196
- worker you can also use ` process.on('message') ` .
192
+ Similar to the ` cluster.on('message') ` event, but specific to this worker.
193
+
194
+ Within a worker, ` process.on('message) ` may also be used.
197
195
198
196
See [ ` process ` event: ` 'message' ` ] [ ] .
199
197
@@ -336,9 +334,9 @@ added: v6.0.0
336
334
337
335
Set by calling ` .kill() ` or ` .disconnect() ` . Until then, it is ` undefined ` .
338
336
339
- The boolean ` worker.exitedAfterDisconnect ` lets you distinguish between voluntary
340
- and accidental exit, the master may choose not to respawn a worker based on
341
- this value.
337
+ The boolean ` worker.exitedAfterDisconnect ` allows distinguishing between
338
+ voluntary and accidental exit, the master may choose not to respawn a worker
339
+ based on this value.
342
340
343
341
``` js
344
342
cluster .on (' exit' , (worker , code , signal ) => {
@@ -369,9 +367,9 @@ cluster.workers
369
367
added: v0.11.14
370
368
-->
371
369
372
- This function returns ` true ` if the worker is connected to its master via its IPC
373
- channel, ` false ` otherwise. A worker is connected to its master after it's been
374
- created. It is disconnected after the ` 'disconnect' ` event is emitted.
370
+ This function returns ` true ` if the worker is connected to its master via its
371
+ IPC channel, ` false ` otherwise. A worker is connected to its master after it
372
+ has been created. It is disconnected after the ` 'disconnect' ` event is emitted.
375
373
376
374
### worker.isDead()
377
375
<!-- YAML
@@ -469,7 +467,7 @@ An alias to [`worker.exitedAfterDisconnect`][].
469
467
470
468
Set by calling ` .kill() ` or ` .disconnect() ` . Until then, it is ` undefined ` .
471
469
472
- The boolean ` worker.suicide ` lets you distinguish between voluntary
470
+ The boolean ` worker.suicide ` is used to distinguish between voluntary
473
471
and accidental exit, the master may choose not to respawn a worker based on
474
472
this value.
475
473
@@ -540,7 +538,7 @@ added: v0.7.0
540
538
* ` worker ` {cluster.Worker}
541
539
542
540
When a new worker is forked the cluster module will emit a ` 'fork' ` event.
543
- This can be used to log worker activity, and create your own timeout.
541
+ This can be used to log worker activity, and create a custom timeout.
544
542
545
543
``` js
546
544
const timeouts = [];
@@ -568,13 +566,14 @@ added: v0.7.0
568
566
* ` worker ` {cluster.Worker}
569
567
* ` address ` {Object}
570
568
571
- After calling ` listen() ` from a worker, when the ` 'listening' ` event is emitted on
572
- the server, a ` 'listening' ` event will also be emitted on ` cluster ` in the master.
569
+ After calling ` listen() ` from a worker, when the ` 'listening' ` event is emitted
570
+ on the server a ` 'listening' ` event will also be emitted on ` cluster ` in the
571
+ master.
573
572
574
- The event handler is executed with two arguments, the ` worker ` contains the worker
575
- object and the ` address ` object contains the following connection properties:
576
- ` address ` , ` port ` and ` addressType ` . This is very useful if the worker is listening
577
- on more than one address.
573
+ The event handler is executed with two arguments, the ` worker ` contains the
574
+ worker object and the ` address ` object contains the following connection
575
+ properties: ` address ` , ` port ` and ` addressType ` . This is very useful if the
576
+ worker is listening on more than one address.
578
577
579
578
``` js
580
579
cluster .on (' listening' , (worker , address ) => {
@@ -610,8 +609,9 @@ See [child_process event: 'message'][].
610
609
Before Node.js v6.0, this event emitted only the message and the handle,
611
610
but not the worker object, contrary to what the documentation stated.
612
611
613
- If you need to support older versions and don't need the worker object,
614
- you can work around the discrepancy by checking the number of arguments:
612
+ If support for older versions is required but a worker object is not
613
+ required, it is possible to work around the discrepancy by checking the
614
+ number of arguments:
615
615
616
616
``` js
617
617
cluster .on (' message' , (worker , message , handle ) => {
@@ -713,8 +713,8 @@ added: v0.11.2
713
713
714
714
The scheduling policy, either ` cluster.SCHED_RR ` for round-robin or
715
715
` cluster.SCHED_NONE ` to leave it to the operating system. This is a
716
- global setting and effectively frozen once you spawn the first worker
717
- or call ` cluster.setupMaster() ` , whatever comes first.
716
+ global setting and effectively frozen once either the first worker is spawned,
717
+ or ` cluster.setupMaster() ` is called, whichever comes first.
718
718
719
719
` SCHED_RR ` is the default on all operating systems except Windows.
720
720
Windows will change to ` SCHED_RR ` once libuv is able to effectively
@@ -750,7 +750,7 @@ changes:
750
750
After calling ` .setupMaster() ` (or ` .fork() ` ) this settings object will contain
751
751
the settings, including the default values.
752
752
753
- This object is not supposed to be changed or set manually, by you .
753
+ This object is not intended to be changed or set manually.
754
754
755
755
## cluster.setupMaster([ settings] )
756
756
<!-- YAML
@@ -850,8 +850,7 @@ eachWorker((worker) => {
850
850
});
851
851
```
852
852
853
- Should you wish to reference a worker over a communication channel, using
854
- the worker's unique id is the easiest way to find the worker.
853
+ Using the worker's unique id is the easiest way to locate the worker.
855
854
856
855
``` js
857
856
socket .on (' data' , (id ) => {
0 commit comments