| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d2e3a15 commit 0fae3d2
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,8 +15,10 @@ const http = require('http'); | |||
| 15 | 15 | const numCPUs = require('os').cpus().length; | |
| 16 | 16 | ||
| 17 | 17 | if (cluster.isMaster) { | |
| 18 | + console.log(`Master ${process.pid} is running`); | ||
| 19 | + | ||
| 18 | 20 | // Fork workers. | |
| 19 | - for (var i = 0; i < numCPUs; i++) { | ||
| 21 | + for (let i = 0; i < numCPUs; i++) { | ||
| 20 | 22 | cluster.fork(); | |
| 21 | 23 | } | |
| 22 | 24 | ||
@@ -30,17 +32,20 @@ if (cluster.isMaster) { | |||
| 30 | 32 | res.writeHead(200); | |
| 31 | 33 | res.end('hello world\n'); | |
| 32 | 34 | }).listen(8000); | |
| 35 | + | ||
| 36 | + console.log(`Worker ${process.pid} started`); | ||
| 33 | 37 | } | |
| 34 | 38 | ``` | |
| 35 | 39 | ||
| 36 | 40 | Running Node.js will now share port 8000 between the workers: | |
| 37 | 41 | ||
| 38 | 42 | ```txt | |
| 39 | - $ NODE_DEBUG=cluster node server.js | ||
| 40 | - 23521,Master Worker 23524 online | ||
| 41 | - 23521,Master Worker 23526 online | ||
| 42 | - 23521,Master Worker 23523 online | ||
| 43 | - 23521,Master Worker 23528 online | ||
| 43 | + $ node server.js | ||
| 44 | + Master 3596 is running | ||
| 45 | + Worker 4324 started | ||
| 46 | + Worker 4520 started | ||
| 47 | + Worker 6056 started | ||
| 48 | + Worker 5644 started | ||
| 44 | 49 | ``` | |
| 45 | 50 | ||
| 46 | 51 | Please note that on Windows, it is not yet possible to set up a named pipe | |
@@ -202,27 +207,27 @@ const http = require('http'); | |||
| 202 | 207 | if (cluster.isMaster) { | |
| 203 | 208 | ||
| 204 | 209 | // Keep track of http requests | |
| 205 | - var numReqs = 0; | ||
| 210 | + let numReqs = 0; | ||
| 206 | 211 | setInterval(() => { | |
| 207 | - console.log('numReqs =', numReqs); | ||
| 212 | + console.log(`numReqs = ${numReqs}`); | ||
| 208 | 213 | }, 1000); | |
| 209 | 214 | ||
| 210 | 215 | // Count requests | |
| 211 | 216 | function messageHandler(msg) { | |
| 212 | - if (msg.cmd && msg.cmd == 'notifyRequest') { | ||
| 217 | + if (msg.cmd && msg.cmd === 'notifyRequest') { | ||
| 213 | 218 | numReqs += 1; | |
| 214 | 219 | } | |
| 215 | 220 | } | |
| 216 | 221 | ||
| 217 | 222 | // Start workers and listen for messages containing notifyRequest | |
| 218 | 223 | const numCPUs = require('os').cpus().length; | |
| 219 | - for (var i = 0; i < numCPUs; i++) { | ||
| 224 | + for (let i = 0; i < numCPUs; i++) { | ||
| 220 | 225 | cluster.fork(); | |
| 221 | 226 | } | |
| 222 | 227 | ||
| 223 | - Object.keys(cluster.workers).forEach((id) => { | ||
| 228 | + for (const id in cluster.workers) { | ||
| 224 | 229 | cluster.workers[id].on('message', messageHandler); | |
| 225 | - }); | ||
| 230 | + } | ||
| 226 | 231 | ||
| 227 | 232 | } else { | |
| 228 | 233 | ||
@@ -285,8 +290,8 @@ the `'disconnect'` event has not been emitted after some time. | |||
| 285 | 290 | ||
| 286 | 291 | ```js | |
| 287 | 292 | if (cluster.isMaster) { | |
| 288 | - var worker = cluster.fork(); | ||
| 289 | - var timeout; | ||
| 293 | + const worker = cluster.fork(); | ||
| 294 | + let timeout; | ||
| 290 | 295 | ||
| 291 | 296 | worker.on('listening', (address) => { | |
| 292 | 297 | worker.send('shutdown'); | |
@@ -302,7 +307,7 @@ if (cluster.isMaster) { | |||
| 302 | 307 | ||
| 303 | 308 | } else if (cluster.isWorker) { | |
| 304 | 309 | const net = require('net'); | |
| 305 | - var server = net.createServer((socket) => { | ||
| 310 | + const server = net.createServer((socket) => { | ||
| 306 | 311 | // connections never end | |
| 307 | 312 | }); | |
| 308 | 313 | ||
@@ -428,7 +433,7 @@ This example will echo back all messages from the master: | |||
| 428 | 433 | ||
| 429 | 434 | ```js | |
| 430 | 435 | if (cluster.isMaster) { | |
| 431 | - var worker = cluster.fork(); | ||
| 436 | + const worker = cluster.fork(); | ||
| 432 | 437 | worker.send('hi there'); | |
| 433 | 438 | ||
| 434 | 439 | } else if (cluster.isWorker) { | |
@@ -524,7 +529,7 @@ When a new worker is forked the cluster module will emit a `'fork'` event. | |||
| 524 | 529 | This can be used to log worker activity, and create your own timeout. | |
| 525 | 530 | ||
| 526 | 531 | ```js | |
| 527 | - var timeouts = []; | ||
| 532 | + const timeouts = []; | ||
| 528 | 533 | function errorMsg() { | |
| 529 | 534 | console.error('Something must be wrong with the connection ...'); | |
| 530 | 535 | } | |
@@ -588,7 +593,7 @@ If you need to support older versions and don't need the worker object, | |||
| 588 | 593 | you can work around the discrepancy by checking the number of arguments: | |
| 589 | 594 | ||
| 590 | 595 | ```js | |
| 591 | - cluster.on('message', function(worker, message, handle) { | ||
| 596 | + cluster.on('message', (worker, message, handle) => { | ||
| 592 | 597 | if (arguments.length === 2) { | |
| 593 | 598 | handle = message; | |
| 594 | 599 | message = worker; | |
@@ -807,7 +812,7 @@ before last `'disconnect'` or `'exit'` event is emitted. | |||
| 807 | 812 | ```js | |
| 808 | 813 | // Go through all workers | |
| 809 | 814 | function eachWorker(callback) { | |
| 810 | - for (var id in cluster.workers) { | ||
| 815 | + for (const id in cluster.workers) { | ||
| 811 | 816 | callback(cluster.workers[id]); | |
| 812 | 817 | } | |
| 813 | 818 | } | |
@@ -821,7 +826,7 @@ the worker's unique id is the easiest way to find the worker. | |||
| 821 | 826 | ||
| 822 | 827 | ```js | |
| 823 | 828 | socket.on('data', (id) => { | |
| 824 | - var worker = cluster.workers[id]; | ||
| 829 | + const worker = cluster.workers[id]; | ||
| 825 | 830 | }); | |
| 826 | 831 | ``` | |
| 827 | 832 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments