| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3c49ee2 commit 15164ce
105 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,8 +18,8 @@ const cluster = require('cluster'); | |||
| 18 | 18 | const http = require('http'); | |
| 19 | 19 | const numCPUs = require('os').cpus().length; | |
| 20 | 20 | ||
| 21 | - if (cluster.isMaster) { | ||
| 22 | - console.log(`Master ${process.pid} is running`); | ||
| 21 | + if (cluster.isPrimary) { | ||
| 22 | + console.log(`Primary ${process.pid} is running`); | ||
| 23 | 23 | ||
| 24 | 24 | // Fork workers. | |
| 25 | 25 | for (let i = 0; i < numCPUs; i++) { | |
@@ -45,7 +45,7 @@ Running Node.js will now share port 8000 between the workers: | |||
| 45 | 45 | ||
| 46 | 46 | ```console | |
| 47 | 47 | $ node server.js | |
| 48 | - Master 3596 is running | ||
| 48 | + Primary 3596 is running | ||
| 49 | 49 | Worker 4324 started | |
| 50 | 50 | Worker 4520 started | |
| 51 | 51 | Worker 6056 started | |
@@ -66,12 +66,12 @@ The cluster module supports two methods of distributing incoming | |||
| 66 | 66 | connections. | |
| 67 | 67 | ||
| 68 | 68 | 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 | ||
| 70 | 70 | port, accepts new connections and distributes them across the workers | |
| 71 | 71 | in a round-robin fashion, with some built-in smarts to avoid | |
| 72 | 72 | overloading a worker process. | |
| 73 | 73 | ||
| 74 | - The second approach is where the master process creates the listen | ||
| 74 | + The second approach is where the primary process creates the listen | ||
| 75 | 75 | socket and sends it to interested workers. The workers then accept | |
| 76 | 76 | incoming connections directly. | |
| 77 | 77 | ||
@@ -81,16 +81,16 @@ to operating system scheduler vagaries. Loads have been observed | |||
| 81 | 81 | where over 70% of all connections ended up in just two processes, | |
| 82 | 82 | out of a total of eight. | |
| 83 | 83 | ||
| 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 | ||
| 85 | 85 | process, there are three cases where the behavior between a normal | |
| 86 | 86 | Node.js process and a cluster worker differs: | |
| 87 | 87 | ||
| 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, | ||
| 89 | 89 | file descriptor 7 **in the parent** will be listened on, and the | |
| 90 | 90 | handle passed to the worker, rather than listening to the worker's | |
| 91 | 91 | idea of what the number 7 file descriptor references. | |
| 92 | 92 | 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 | ||
| 94 | 94 | process. | |
| 95 | 95 | 3. `server.listen(0)` Normally, this will cause servers to listen on a | |
| 96 | 96 | random port. However, in a cluster, each worker will receive the | |
@@ -121,7 +121,7 @@ added: v0.7.0 | |||
| 121 | 121 | * Extends: {EventEmitter} | |
| 122 | 122 | ||
| 123 | 123 | 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 | ||
| 125 | 125 | it can be obtained using `cluster.worker`. | |
| 126 | 126 | ||
| 127 | 127 | ### Event: `'disconnect'` | |
@@ -201,14 +201,14 @@ Within a worker, `process.on('message')` may also be used. | |||
| 201 | 201 | ||
| 202 | 202 | See [`process` event: `'message'`][]. | |
| 203 | 203 | ||
| 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 | ||
| 205 | 205 | process of the number of HTTP requests received by the workers: | |
| 206 | 206 | ||
| 207 | 207 | ```js | |
| 208 | 208 | const cluster = require('cluster'); | |
| 209 | 209 | const http = require('http'); | |
| 210 | 210 | ||
| 211 | - if (cluster.isMaster) { | ||
| 211 | + if (cluster.isPrimary) { | ||
| 212 | 212 | ||
| 213 | 213 | // Keep track of http requests | |
| 214 | 214 | let numReqs = 0; | |
@@ -240,7 +240,7 @@ if (cluster.isMaster) { | |||
| 240 | 240 | res.writeHead(200); | |
| 241 | 241 | res.end('hello world\n'); | |
| 242 | 242 | ||
| 243 | - // Notify master about the request | ||
| 243 | + // Notify primary about the request | ||
| 244 | 244 | process.send({ cmd: 'notifyRequest' }); | |
| 245 | 245 | }).listen(8000); | |
| 246 | 246 | } | |
@@ -275,7 +275,7 @@ changes: | |||
| 275 | 275 | In a worker, this function will close all servers, wait for the `'close'` event | |
| 276 | 276 | on those servers, and then disconnect the IPC channel. | |
| 277 | 277 | ||
| 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 | ||
| 279 | 279 | `.disconnect()` on itself. | |
| 280 | 280 | ||
| 281 | 281 | Causes `.exitedAfterDisconnect` to be set. | |
@@ -299,7 +299,7 @@ close them. It also may be useful to implement a timeout, killing a worker if | |||
| 299 | 299 | the `'disconnect'` event has not been emitted after some time. | |
| 300 | 300 | ||
| 301 | 301 | ```js | |
| 302 | - if (cluster.isMaster) { | ||
| 302 | + if (cluster.isPrimary) { | ||
| 303 | 303 | const worker = cluster.fork(); | |
| 304 | 304 | let timeout; | |
| 305 | 305 | ||
@@ -343,7 +343,7 @@ This property is `true` if the worker exited due to `.kill()` or | |||
| 343 | 343 | worker has not exited, it is `undefined`. | |
| 344 | 344 | ||
| 345 | 345 | 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 | ||
| 347 | 347 | based on this value. | |
| 348 | 348 | ||
| 349 | 349 | ```js | |
@@ -375,8 +375,8 @@ While a worker is alive, this is the key that indexes it in | |||
| 375 | 375 | added: v0.11.14 | |
| 376 | 376 | --> | |
| 377 | 377 | ||
| 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 | ||
| 380 | 380 | has been created. It is disconnected after the `'disconnect'` event is emitted. | |
| 381 | 381 | ||
| 382 | 382 | ### `worker.isDead()` | |
@@ -392,8 +392,8 @@ const cluster = require('cluster'); | |||
| 392 | 392 | const http = require('http'); | |
| 393 | 393 | const numCPUs = require('os').cpus().length; | |
| 394 | 394 | ||
| 395 | - if (cluster.isMaster) { | ||
| 396 | - console.log(`Master ${process.pid} is running`); | ||
| 395 | + if (cluster.isPrimary) { | ||
| 396 | + console.log(`Primary ${process.pid} is running`); | ||
| 397 | 397 | ||
| 398 | 398 | // Fork workers. | |
| 399 | 399 | for (let i = 0; i < numCPUs; i++) { | |
@@ -425,9 +425,10 @@ added: v0.9.12 | |||
| 425 | 425 | * `signal` {string} Name of the kill signal to send to the worker | |
| 426 | 426 | process. **Default**: `'SIGTERM'` | |
| 427 | 427 | ||
| 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`. | ||
| 431 | 432 | ||
| 432 | 433 | Because `kill()` attempts to gracefully disconnect the worker process, it is | |
| 433 | 434 | susceptible to waiting indefinitely for the disconnect to complete. For example, | |
@@ -478,18 +479,18 @@ changes: | |||
| 478 | 479 | * `callback` {Function} | |
| 479 | 480 | * Returns: {boolean} | |
| 480 | 481 | ||
| 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. | ||
| 482 | 483 | ||
| 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 | ||
| 484 | 485 | [`ChildProcess.send()`][]. | |
| 485 | 486 | ||
| 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 | ||
| 487 | 488 | `process.send()`. | |
| 488 | 489 | ||
| 489 | - This example will echo back all messages from the master: | ||
| 490 | + This example will echo back all messages from the primary: | ||
| 490 | 491 | ||
| 491 | 492 | ```js | |
| 492 | - if (cluster.isMaster) { | ||
| 493 | + if (cluster.isPrimary) { | ||
| 493 | 494 | const worker = cluster.fork(); | |
| 494 | 495 | worker.send('hi there'); | |
| 495 | 496 | ||
@@ -583,7 +584,7 @@ added: v0.7.0 | |||
| 583 | 584 | ||
| 584 | 585 | After calling `listen()` from a worker, when the `'listening'` event is emitted | |
| 585 | 586 | on the server a `'listening'` event will also be emitted on `cluster` in the | |
| 586 | - master. | ||
| 587 | + primary. | ||
| 587 | 588 | ||
| 588 | 589 | The event handler is executed with two arguments, the `worker` contains the | |
| 589 | 590 | worker object and the `address` object contains the following connection | |
@@ -617,7 +618,7 @@ changes: | |||
| 617 | 618 | * `message` {Object} | |
| 618 | 619 | * `handle` {undefined|Object} | |
| 619 | 620 | ||
| 620 | - Emitted when the cluster master receives a message from any worker. | ||
| 621 | + Emitted when the cluster primary receives a message from any worker. | ||
| 621 | 622 | ||
| 622 | 623 | See [`child_process` event: `'message'`][]. | |
| 623 | 624 | ||
@@ -629,9 +630,9 @@ added: v0.7.0 | |||
| 629 | 630 | * `worker` {cluster.Worker} | |
| 630 | 631 | ||
| 631 | 632 | 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. | ||
| 633 | 634 | 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. | ||
| 635 | 636 | ||
| 636 | 637 | ```js | |
| 637 | 638 | cluster.on('online', (worker) => { | |
@@ -646,11 +647,11 @@ added: v0.7.1 | |||
| 646 | 647 | ||
| 647 | 648 | * `settings` {Object} | |
| 648 | 649 | ||
| 649 | - Emitted every time [`.setupMaster()`][] is called. | ||
| 650 | + Emitted every time [`.setupPrimary()`][] is called. | ||
| 650 | 651 | ||
| 651 | 652 | 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. | ||
| 654 | 655 | ||
| 655 | 656 | If accuracy is important, use `cluster.settings`. | |
| 656 | 657 | ||
@@ -665,12 +666,12 @@ added: v0.7.7 | |||
| 665 | 666 | Calls `.disconnect()` on each worker in `cluster.workers`. | |
| 666 | 667 | ||
| 667 | 668 | 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. | ||
| 669 | 670 | ||
| 670 | 671 | The method takes an optional callback argument which will be called when | |
| 671 | 672 | finished. | |
| 672 | 673 | ||
| 673 | - This can only be called from the master process. | ||
| 674 | + This can only be called from the primary process. | ||
| 674 | 675 | ||
| 675 | 676 | ## `cluster.fork([env])` | |
| 676 | 677 | <!-- YAML | |
@@ -682,18 +683,27 @@ added: v0.6.0 | |||
| 682 | 683 | ||
| 683 | 684 | Spawn a new worker process. | |
| 684 | 685 | ||
| 685 | - This can only be called from the master process. | ||
| 686 | + This can only be called from the primary process. | ||
| 686 | 687 | ||
| 687 | 688 | ## `cluster.isMaster` | |
| 688 | 689 | <!-- YAML | |
| 689 | 690 | 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 | ||
| 690 | 700 | --> | |
| 691 | 701 | ||
| 692 | 702 | * {boolean} | |
| 693 | 703 | ||
| 694 | - True if the process is a master. This is determined | ||
| 704 | + True if the process is a primary. This is determined | ||
| 695 | 705 | 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`. | ||
| 697 | 707 | ||
| 698 | 708 | ## `cluster.isWorker` | |
| 699 | 709 | <!-- YAML | |
@@ -702,7 +712,7 @@ added: v0.6.0 | |||
| 702 | 712 | ||
| 703 | 713 | * {boolean} | |
| 704 | 714 | ||
| 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`). | ||
| 706 | 716 | ||
| 707 | 717 | ## `cluster.schedulingPolicy` | |
| 708 | 718 | <!-- YAML | |
@@ -712,7 +722,7 @@ added: v0.11.2 | |||
| 712 | 722 | The scheduling policy, either `cluster.SCHED_RR` for round-robin or | |
| 713 | 723 | `cluster.SCHED_NONE` to leave it to the operating system. This is a | |
| 714 | 724 | 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. | ||
| 716 | 726 | ||
| 717 | 727 | `SCHED_RR` is the default on all operating systems except Windows. | |
| 718 | 728 | Windows will change to `SCHED_RR` once libuv is able to effectively | |
@@ -767,54 +777,62 @@ changes: | |||
| 767 | 777 | * `inspectPort` {number|Function} Sets inspector port of worker. | |
| 768 | 778 | This can be a number, or a function that takes no arguments and returns a | |
| 769 | 779 | number. By default each worker gets its own port, incremented from the | |
| 770 | - master's `process.debugPort`. | ||
| 780 | + primary's `process.debugPort`. | ||
| 771 | 781 | * `windowsHide` {boolean} Hide the forked processes console window that would | |
| 772 | 782 | normally be created on Windows systems. **Default:** `false`. | |
| 773 | 783 | ||
| 774 | - After calling [`.setupMaster()`][] (or [`.fork()`][]) this settings object will | ||
| 784 | + After calling [`.setupPrimary()`][] (or [`.fork()`][]) this settings object will | ||
| 775 | 785 | contain the settings, including the default values. | |
| 776 | 786 | ||
| 777 | 787 | This object is not intended to be changed or set manually. | |
| 778 | 788 | ||
| 779 | 789 | ## `cluster.setupMaster([settings])` | |
| 780 | 790 | <!-- YAML | |
| 781 | 791 | added: v0.7.1 | |
| 792 | + deprecated: REPLACEME | ||
| 782 | 793 | changes: | |
| 783 | 794 | - version: v6.4.0 | |
| 784 | 795 | pr-url: https://github.com/nodejs/node/pull/7838 | |
| 785 | 796 | description: The `stdio` option is supported now. | |
| 786 | 797 | --> | |
| 787 | 798 | ||
| 799 | + Deprecated alias for [`.setupPrimary()`][]. | ||
| 800 | + | ||
| 801 | + ## `cluster.setupPrimary([settings])` | ||
| 802 | + <!-- YAML | ||
| 803 | + added: REPLACEME | ||
| 804 | + --> | ||
| 805 | + | ||
| 788 | 806 | * `settings` {Object} See [`cluster.settings`][]. | |
| 789 | 807 | ||
| 790 | - `setupMaster` is used to change the default 'fork' behavior. Once called, | ||
| 808 | + `setupPrimary` is used to change the default 'fork' behavior. Once called, | ||
| 791 | 809 | the settings will be present in `cluster.settings`. | |
| 792 | 810 | ||
| 793 | 811 | Any settings changes only affect future calls to [`.fork()`][] and have no | |
| 794 | 812 | effect on workers that are already running. | |
| 795 | 813 | ||
| 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 | ||
| 797 | 815 | the `env` passed to [`.fork()`][]. | |
| 798 | 816 | ||
| 799 | 817 | 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. | ||
| 801 | 819 | ||
| 802 | 820 | ```js | |
| 803 | 821 | const cluster = require('cluster'); | |
| 804 | - cluster.setupMaster({ | ||
| 822 | + cluster.setupPrimary({ | ||
| 805 | 823 | exec: 'worker.js', | |
| 806 | 824 | args: ['--use', 'https'], | |
| 807 | 825 | silent: true | |
| 808 | 826 | }); | |
| 809 | 827 | cluster.fork(); // https worker | |
| 810 | - cluster.setupMaster({ | ||
| 828 | + cluster.setupPrimary({ | ||
| 811 | 829 | exec: 'worker.js', | |
| 812 | 830 | args: ['--use', 'http'] | |
| 813 | 831 | }); | |
| 814 | 832 | cluster.fork(); // http worker | |
| 815 | 833 | ``` | |
| 816 | 834 | ||
| 817 | - This can only be called from the master process. | ||
| 835 | + This can only be called from the primary process. | ||
| 818 | 836 | ||
| 819 | 837 | ## `cluster.worker` | |
| 820 | 838 | <!-- YAML | |
@@ -823,13 +841,13 @@ added: v0.7.0 | |||
| 823 | 841 | ||
| 824 | 842 | * {Object} | |
| 825 | 843 | ||
| 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. | ||
| 827 | 845 | ||
| 828 | 846 | ```js | |
| 829 | 847 | const cluster = require('cluster'); | |
| 830 | 848 | ||
| 831 | - if (cluster.isMaster) { | ||
| 832 | - console.log('I am master'); | ||
| 849 | + if (cluster.isPrimary) { | ||
| 850 | + console.log('I am primary'); | ||
| 833 | 851 | cluster.fork(); | |
| 834 | 852 | cluster.fork(); | |
| 835 | 853 | } else if (cluster.isWorker) { | |
@@ -845,7 +863,7 @@ added: v0.7.0 | |||
| 845 | 863 | * {Object} | |
| 846 | 864 | ||
| 847 | 865 | 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 | ||
| 849 | 867 | process. | |
| 850 | 868 | ||
| 851 | 869 | A worker is removed from `cluster.workers` after the worker has disconnected | |
@@ -876,13 +894,14 @@ socket.on('data', (id) => { | |||
| 876 | 894 | [Advanced serialization for `child_process`]: child_process.md#child_process_advanced_serialization | |
| 877 | 895 | [Child Process module]: child_process.md#child_process_child_process_fork_modulepath_args_options | |
| 878 | 896 | [`.fork()`]: #cluster_cluster_fork_env | |
| 879 | - [`.setupMaster()`]: #cluster_cluster_setupmaster_settings | ||
| 897 | + [`.setupPrimary()`]: #cluster_cluster_setupprimary_settings | ||
| 880 | 898 | [`ChildProcess.send()`]: child_process.md#child_process_subprocess_send_message_sendhandle_options_callback | |
| 881 | 899 | [`child_process.fork()`]: child_process.md#child_process_child_process_fork_modulepath_args_options | |
| 882 | 900 | [`child_process` event: `'exit'`]: child_process.md#child_process_event_exit | |
| 883 | 901 | [`child_process` event: `'message'`]: child_process.md#child_process_event_message | |
| 884 | 902 | [`cluster.settings`]: #cluster_cluster_settings | |
| 885 | 903 | [`disconnect()`]: child_process.md#child_process_subprocess_disconnect | |
| 904 | + [`cluster.isPrimary`]: #cluster_cluster_isprimary | ||
| 886 | 905 | [`kill()`]: process.md#process_process_kill_pid_signal | |
| 887 | 906 | [`process` event: `'message'`]: process.md#process_event_message | |
| 888 | 907 | [`server.close()`]: net.md#net_event_close | |
| Back | FazBrowse Home | New Git URL |
0 commit comments