Skip to content

Commit 15164ce

Browse files
committed
lib,src: update cluster to use Parent
Doc deprecate isMaster and setupMaster in favor of isPrimary and setupPrimary. Signed-off-by: Michael Dawson <[email protected]> PR-URL: #36478 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Beth Griggs <[email protected]>
1 parent 3c49ee2 commit 15164ce

File tree

105 files changed

+341
-315
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+341
-315
lines changed

Diff for: doc/api/cluster.md

+73-54
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const cluster = require('cluster');
1818
const http = require('http');
1919
const numCPUs = require('os').cpus().length;
2020

21-
if (cluster.isMaster) {
22-
console.log(`Master ${process.pid} is running`);
21+
if (cluster.isPrimary) {
22+
console.log(`Primary ${process.pid} is running`);
2323

2424
// Fork workers.
2525
for (let i = 0; i < numCPUs; i++) {
@@ -45,7 +45,7 @@ Running Node.js will now share port 8000 between the workers:
4545

4646
```console
4747
$ node server.js
48-
Master 3596 is running
48+
Primary 3596 is running
4949
Worker 4324 started
5050
Worker 4520 started
5151
Worker 6056 started
@@ -66,12 +66,12 @@ The cluster module supports two methods of distributing incoming
6666
connections.
6767

6868
The first one (and the default one on all platforms except Windows),
69-
is the round-robin approach, where the master process listens on a
69+
is the round-robin approach, where the primary process listens on a
7070
port, accepts new connections and distributes them across the workers
7171
in a round-robin fashion, with some built-in smarts to avoid
7272
overloading a worker process.
7373

74-
The second approach is where the master process creates the listen
74+
The second approach is where the primary process creates the listen
7575
socket and sends it to interested workers. The workers then accept
7676
incoming connections directly.
7777

@@ -81,16 +81,16 @@ to operating system scheduler vagaries. Loads have been observed
8181
where over 70% of all connections ended up in just two processes,
8282
out of a total of eight.
8383

84-
Because `server.listen()` hands off most of the work to the master
84+
Because `server.listen()` hands off most of the work to the primary
8585
process, there are three cases where the behavior between a normal
8686
Node.js process and a cluster worker differs:
8787

88-
1. `server.listen({fd: 7})` Because the message is passed to the master,
88+
1. `server.listen({fd: 7})` Because the message is passed to the primary,
8989
file descriptor 7 **in the parent** will be listened on, and the
9090
handle passed to the worker, rather than listening to the worker's
9191
idea of what the number 7 file descriptor references.
9292
2. `server.listen(handle)` Listening on handles explicitly will cause
93-
the worker to use the supplied handle, rather than talk to the master
93+
the worker to use the supplied handle, rather than talk to the primary
9494
process.
9595
3. `server.listen(0)` Normally, this will cause servers to listen on a
9696
random port. However, in a cluster, each worker will receive the
@@ -121,7 +121,7 @@ added: v0.7.0
121121
* Extends: {EventEmitter}
122122

123123
A `Worker` object contains all public information and method about a worker.
124-
In the master it can be obtained using `cluster.workers`. In a worker
124+
In the primary it can be obtained using `cluster.workers`. In a worker
125125
it can be obtained using `cluster.worker`.
126126

127127
### Event: `'disconnect'`
@@ -201,14 +201,14 @@ Within a worker, `process.on('message')` may also be used.
201201

202202
See [`process` event: `'message'`][].
203203

204-
Here is an example using the message system. It keeps a count in the master
204+
Here is an example using the message system. It keeps a count in the primary
205205
process of the number of HTTP requests received by the workers:
206206

207207
```js
208208
const cluster = require('cluster');
209209
const http = require('http');
210210

211-
if (cluster.isMaster) {
211+
if (cluster.isPrimary) {
212212

213213
// Keep track of http requests
214214
let numReqs = 0;
@@ -240,7 +240,7 @@ if (cluster.isMaster) {
240240
res.writeHead(200);
241241
res.end('hello world\n');
242242

243-
// Notify master about the request
243+
// Notify primary about the request
244244
process.send({ cmd: 'notifyRequest' });
245245
}).listen(8000);
246246
}
@@ -275,7 +275,7 @@ changes:
275275
In a worker, this function will close all servers, wait for the `'close'` event
276276
on those servers, and then disconnect the IPC channel.
277277

278-
In the master, an internal message is sent to the worker causing it to call
278+
In the primary, an internal message is sent to the worker causing it to call
279279
`.disconnect()` on itself.
280280

281281
Causes `.exitedAfterDisconnect` to be set.
@@ -299,7 +299,7 @@ close them. It also may be useful to implement a timeout, killing a worker if
299299
the `'disconnect'` event has not been emitted after some time.
300300

301301
```js
302-
if (cluster.isMaster) {
302+
if (cluster.isPrimary) {
303303
const worker = cluster.fork();
304304
let timeout;
305305

@@ -343,7 +343,7 @@ This property is `true` if the worker exited due to `.kill()` or
343343
worker has not exited, it is `undefined`.
344344

345345
The boolean [`worker.exitedAfterDisconnect`][] allows distinguishing between
346-
voluntary and accidental exit, the master may choose not to respawn a worker
346+
voluntary and accidental exit, the primary may choose not to respawn a worker
347347
based on this value.
348348

349349
```js
@@ -375,8 +375,8 @@ While a worker is alive, this is the key that indexes it in
375375
added: v0.11.14
376376
-->
377377

378-
This function returns `true` if the worker is connected to its master via its
379-
IPC channel, `false` otherwise. A worker is connected to its master after it
378+
This function returns `true` if the worker is connected to its primary via its
379+
IPC channel, `false` otherwise. A worker is connected to its primary after it
380380
has been created. It is disconnected after the `'disconnect'` event is emitted.
381381

382382
### `worker.isDead()`
@@ -392,8 +392,8 @@ const cluster = require('cluster');
392392
const http = require('http');
393393
const numCPUs = require('os').cpus().length;
394394

395-
if (cluster.isMaster) {
396-
console.log(`Master ${process.pid} is running`);
395+
if (cluster.isPrimary) {
396+
console.log(`Primary ${process.pid} is running`);
397397

398398
// Fork workers.
399399
for (let i = 0; i < numCPUs; i++) {
@@ -425,9 +425,10 @@ added: v0.9.12
425425
* `signal` {string} Name of the kill signal to send to the worker
426426
process. **Default**: `'SIGTERM'`
427427

428-
This function will kill the worker. In the master, it does this by disconnecting
429-
the `worker.process`, and once disconnected, killing with `signal`. In the
430-
worker, it does it by disconnecting the channel, and then exiting with code `0`.
428+
This function will kill the worker. In the primary, it does this
429+
by disconnecting the `worker.process`, and once disconnected, killing
430+
with `signal`. In the worker, it does it by disconnecting the channel,
431+
and then exiting with code `0`.
431432

432433
Because `kill()` attempts to gracefully disconnect the worker process, it is
433434
susceptible to waiting indefinitely for the disconnect to complete. For example,
@@ -478,18 +479,18 @@ changes:
478479
* `callback` {Function}
479480
* Returns: {boolean}
480481

481-
Send a message to a worker or master, optionally with a handle.
482+
Send a message to a worker or primary, optionally with a handle.
482483

483-
In the master this sends a message to a specific worker. It is identical to
484+
In the primary this sends a message to a specific worker. It is identical to
484485
[`ChildProcess.send()`][].
485486

486-
In a worker this sends a message to the master. It is identical to
487+
In a worker this sends a message to the primary. It is identical to
487488
`process.send()`.
488489

489-
This example will echo back all messages from the master:
490+
This example will echo back all messages from the primary:
490491

491492
```js
492-
if (cluster.isMaster) {
493+
if (cluster.isPrimary) {
493494
const worker = cluster.fork();
494495
worker.send('hi there');
495496

@@ -583,7 +584,7 @@ added: v0.7.0
583584

584585
After calling `listen()` from a worker, when the `'listening'` event is emitted
585586
on the server a `'listening'` event will also be emitted on `cluster` in the
586-
master.
587+
primary.
587588

588589
The event handler is executed with two arguments, the `worker` contains the
589590
worker object and the `address` object contains the following connection
@@ -617,7 +618,7 @@ changes:
617618
* `message` {Object}
618619
* `handle` {undefined|Object}
619620

620-
Emitted when the cluster master receives a message from any worker.
621+
Emitted when the cluster primary receives a message from any worker.
621622

622623
See [`child_process` event: `'message'`][].
623624

@@ -629,9 +630,9 @@ added: v0.7.0
629630
* `worker` {cluster.Worker}
630631

631632
After forking a new worker, the worker should respond with an online message.
632-
When the master receives an online message it will emit this event.
633+
When the primary receives an online message it will emit this event.
633634
The difference between `'fork'` and `'online'` is that fork is emitted when the
634-
master forks a worker, and `'online'` is emitted when the worker is running.
635+
primary forks a worker, and `'online'` is emitted when the worker is running.
635636

636637
```js
637638
cluster.on('online', (worker) => {
@@ -646,11 +647,11 @@ added: v0.7.1
646647

647648
* `settings` {Object}
648649

649-
Emitted every time [`.setupMaster()`][] is called.
650+
Emitted every time [`.setupPrimary()`][] is called.
650651

651652
The `settings` object is the `cluster.settings` object at the time
652-
[`.setupMaster()`][] was called and is advisory only, since multiple calls to
653-
[`.setupMaster()`][] can be made in a single tick.
653+
[`.setupPrimary()`][] was called and is advisory only, since multiple calls to
654+
[`.setupPrimary()`][] can be made in a single tick.
654655

655656
If accuracy is important, use `cluster.settings`.
656657

@@ -665,12 +666,12 @@ added: v0.7.7
665666
Calls `.disconnect()` on each worker in `cluster.workers`.
666667

667668
When they are disconnected all internal handles will be closed, allowing the
668-
master process to die gracefully if no other event is waiting.
669+
primary process to die gracefully if no other event is waiting.
669670

670671
The method takes an optional callback argument which will be called when
671672
finished.
672673

673-
This can only be called from the master process.
674+
This can only be called from the primary process.
674675

675676
## `cluster.fork([env])`
676677
<!-- YAML
@@ -682,18 +683,27 @@ added: v0.6.0
682683

683684
Spawn a new worker process.
684685

685-
This can only be called from the master process.
686+
This can only be called from the primary process.
686687

687688
## `cluster.isMaster`
688689
<!-- YAML
689690
added: v0.8.1
691+
deprecated: REPLACEME
692+
-->
693+
694+
Deprecated alias for [`cluster.isPrimary`][].
695+
details.
696+
697+
## `cluster.isPrimary`
698+
<!-- YAML
699+
added: REPLACEME
690700
-->
691701

692702
* {boolean}
693703

694-
True if the process is a master. This is determined
704+
True if the process is a primary. This is determined
695705
by the `process.env.NODE_UNIQUE_ID`. If `process.env.NODE_UNIQUE_ID` is
696-
undefined, then `isMaster` is `true`.
706+
undefined, then `isPrimary` is `true`.
697707

698708
## `cluster.isWorker`
699709
<!-- YAML
@@ -702,7 +712,7 @@ added: v0.6.0
702712

703713
* {boolean}
704714

705-
True if the process is not a master (it is the negation of `cluster.isMaster`).
715+
True if the process is not a primary (it is the negation of `cluster.isPrimary`).
706716

707717
## `cluster.schedulingPolicy`
708718
<!-- YAML
@@ -712,7 +722,7 @@ added: v0.11.2
712722
The scheduling policy, either `cluster.SCHED_RR` for round-robin or
713723
`cluster.SCHED_NONE` to leave it to the operating system. This is a
714724
global setting and effectively frozen once either the first worker is spawned,
715-
or [`.setupMaster()`][] is called, whichever comes first.
725+
or [`.setupPrimary()`][] is called, whichever comes first.
716726

717727
`SCHED_RR` is the default on all operating systems except Windows.
718728
Windows will change to `SCHED_RR` once libuv is able to effectively
@@ -767,54 +777,62 @@ changes:
767777
* `inspectPort` {number|Function} Sets inspector port of worker.
768778
This can be a number, or a function that takes no arguments and returns a
769779
number. By default each worker gets its own port, incremented from the
770-
master's `process.debugPort`.
780+
primary's `process.debugPort`.
771781
* `windowsHide` {boolean} Hide the forked processes console window that would
772782
normally be created on Windows systems. **Default:** `false`.
773783

774-
After calling [`.setupMaster()`][] (or [`.fork()`][]) this settings object will
784+
After calling [`.setupPrimary()`][] (or [`.fork()`][]) this settings object will
775785
contain the settings, including the default values.
776786

777787
This object is not intended to be changed or set manually.
778788

779789
## `cluster.setupMaster([settings])`
780790
<!-- YAML
781791
added: v0.7.1
792+
deprecated: REPLACEME
782793
changes:
783794
- version: v6.4.0
784795
pr-url: https://github.com/nodejs/node/pull/7838
785796
description: The `stdio` option is supported now.
786797
-->
787798

799+
Deprecated alias for [`.setupPrimary()`][].
800+
801+
## `cluster.setupPrimary([settings])`
802+
<!-- YAML
803+
added: REPLACEME
804+
-->
805+
788806
* `settings` {Object} See [`cluster.settings`][].
789807

790-
`setupMaster` is used to change the default 'fork' behavior. Once called,
808+
`setupPrimary` is used to change the default 'fork' behavior. Once called,
791809
the settings will be present in `cluster.settings`.
792810

793811
Any settings changes only affect future calls to [`.fork()`][] and have no
794812
effect on workers that are already running.
795813

796-
The only attribute of a worker that cannot be set via `.setupMaster()` is
814+
The only attribute of a worker that cannot be set via `.setupPrimary()` is
797815
the `env` passed to [`.fork()`][].
798816

799817
The defaults above apply to the first call only; the defaults for later
800-
calls are the current values at the time of `cluster.setupMaster()` is called.
818+
calls are the current values at the time of `cluster.setupPrimary()` is called.
801819

802820
```js
803821
const cluster = require('cluster');
804-
cluster.setupMaster({
822+
cluster.setupPrimary({
805823
exec: 'worker.js',
806824
args: ['--use', 'https'],
807825
silent: true
808826
});
809827
cluster.fork(); // https worker
810-
cluster.setupMaster({
828+
cluster.setupPrimary({
811829
exec: 'worker.js',
812830
args: ['--use', 'http']
813831
});
814832
cluster.fork(); // http worker
815833
```
816834

817-
This can only be called from the master process.
835+
This can only be called from the primary process.
818836

819837
## `cluster.worker`
820838
<!-- YAML
@@ -823,13 +841,13 @@ added: v0.7.0
823841

824842
* {Object}
825843

826-
A reference to the current worker object. Not available in the master process.
844+
A reference to the current worker object. Not available in the primary process.
827845

828846
```js
829847
const cluster = require('cluster');
830848

831-
if (cluster.isMaster) {
832-
console.log('I am master');
849+
if (cluster.isPrimary) {
850+
console.log('I am primary');
833851
cluster.fork();
834852
cluster.fork();
835853
} else if (cluster.isWorker) {
@@ -845,7 +863,7 @@ added: v0.7.0
845863
* {Object}
846864

847865
A hash that stores the active worker objects, keyed by `id` field. Makes it
848-
easy to loop through all the workers. It is only available in the master
866+
easy to loop through all the workers. It is only available in the primary
849867
process.
850868

851869
A worker is removed from `cluster.workers` after the worker has disconnected
@@ -876,13 +894,14 @@ socket.on('data', (id) => {
876894
[Advanced serialization for `child_process`]: child_process.md#child_process_advanced_serialization
877895
[Child Process module]: child_process.md#child_process_child_process_fork_modulepath_args_options
878896
[`.fork()`]: #cluster_cluster_fork_env
879-
[`.setupMaster()`]: #cluster_cluster_setupmaster_settings
897+
[`.setupPrimary()`]: #cluster_cluster_setupprimary_settings
880898
[`ChildProcess.send()`]: child_process.md#child_process_subprocess_send_message_sendhandle_options_callback
881899
[`child_process.fork()`]: child_process.md#child_process_child_process_fork_modulepath_args_options
882900
[`child_process` event: `'exit'`]: child_process.md#child_process_event_exit
883901
[`child_process` event: `'message'`]: child_process.md#child_process_event_message
884902
[`cluster.settings`]: #cluster_cluster_settings
885903
[`disconnect()`]: child_process.md#child_process_subprocess_disconnect
904+
[`cluster.isPrimary`]: #cluster_cluster_isprimary
886905
[`kill()`]: process.md#process_process_kill_pid_signal
887906
[`process` event: `'message'`]: process.md#process_event_message
888907
[`server.close()`]: net.md#net_event_close

0 commit comments

Comments
 (0)