| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 48c2783 commit f8a676e
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -428,7 +428,7 @@ function masterInit() { | |||
| 428 | 428 | else if (message.act === 'listening') | |
| 429 | 429 | listening(worker, message); | |
| 430 | 430 | else if (message.act === 'suicide') | |
| 431 | - worker.suicide = true; | ||
| 431 | + suicide(worker, message); | ||
| 432 | 432 | else if (message.act === 'close') | |
| 433 | 433 | close(worker, message); | |
| 434 | 434 | } | |
@@ -439,6 +439,11 @@ function masterInit() { | |||
| 439 | 439 | cluster.emit('online', worker); | |
| 440 | 440 | } | |
| 441 | 441 | ||
| 442 | + function suicide(worker, message) { | ||
| 443 | + worker.suicide = true; | ||
| 444 | + send(worker, { ack: message.seq }); | ||
| 445 | + } | ||
| 446 | + | ||
| 442 | 447 | function queryServer(worker, message) { | |
| 443 | 448 | var args = [message.address, | |
| 444 | 449 | message.port, | |
@@ -532,7 +537,7 @@ function workerInit() { | |||
| 532 | 537 | if (message.act === 'newconn') | |
| 533 | 538 | onconnection(message, handle); | |
| 534 | 539 | else if (message.act === 'disconnect') | |
| 535 | - worker.disconnect(); | ||
| 540 | + _disconnect.call(worker, true); | ||
| 536 | 541 | } | |
| 537 | 542 | }; | |
| 538 | 543 | ||
@@ -653,14 +658,36 @@ function workerInit() { | |||
| 653 | 658 | } | |
| 654 | 659 | ||
| 655 | 660 | Worker.prototype.disconnect = function() { | |
| 661 | + _disconnect.call(this); | ||
| 662 | + }; | ||
| 663 | + | ||
| 664 | + Worker.prototype.destroy = function() { | ||
| 665 | + this.suicide = true; | ||
| 666 | + if (!this.isConnected()) process.exit(0); | ||
| 667 | + var exit = process.exit.bind(null, 0); | ||
| 668 | + send({ act: 'suicide' }, () => process.disconnect()); | ||
| 669 | + process.once('disconnect', exit); | ||
| 670 | + }; | ||
| 671 | + | ||
| 672 | + function send(message, cb) { | ||
| 673 | + sendHelper(process, message, null, cb); | ||
| 674 | + } | ||
| 675 | + | ||
| 676 | + function _disconnect(masterInitiated) { | ||
| 656 | 677 | this.suicide = true; | |
| 657 | 678 | let waitingCount = 1; | |
| 658 | 679 | ||
| 659 | 680 | function checkWaitingCount() { | |
| 660 | 681 | waitingCount--; | |
| 661 | 682 | if (waitingCount === 0) { | |
| 662 | - send({ act: 'suicide' }); | ||
| 663 | - process.disconnect(); | ||
| 683 | + // If disconnect is worker initiated, wait for ack to be sure suicide | ||
| 684 | + // is properly set in the master, otherwise, if it's master initiated | ||
| 685 | + // there's no need to send the suicide message | ||
| 686 | + if (masterInitiated) { | ||
| 687 | + process.disconnect(); | ||
| 688 | + } else { | ||
| 689 | + send({ act: 'suicide' }, () => process.disconnect()); | ||
| 690 | + } | ||
| 664 | 691 | } | |
| 665 | 692 | } | |
| 666 | 693 | ||
@@ -672,19 +699,6 @@ function workerInit() { | |||
| 672 | 699 | } | |
| 673 | 700 | ||
| 674 | 701 | checkWaitingCount(); | |
| 675 | - }; | ||
| 676 | - | ||
| 677 | - Worker.prototype.destroy = function() { | ||
| 678 | - this.suicide = true; | ||
| 679 | - if (!this.isConnected()) process.exit(0); | ||
| 680 | - var exit = process.exit.bind(null, 0); | ||
| 681 | - send({ act: 'suicide' }, exit); | ||
| 682 | - process.once('disconnect', exit); | ||
| 683 | - process.disconnect(); | ||
| 684 | - }; | ||
| 685 | - | ||
| 686 | - function send(message, cb) { | ||
| 687 | - sendHelper(process, message, null, cb); | ||
| 688 | 702 | } | |
| 689 | 703 | } | |
| 690 | 704 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,18 +4,19 @@ const assert = require('assert'); | |||
| 4 | 4 | const cluster = require('cluster'); | |
| 5 | 5 | ||
| 6 | 6 | if (cluster.isMaster) { | |
| 7 | - const worker = cluster.fork(); | ||
| 8 | - let disconnected = false; | ||
| 7 | + function forkWorker(action) { | ||
| 8 | + const worker = cluster.fork({ action }); | ||
| 9 | + worker.on('disconnect', common.mustCall(() => { | ||
| 10 | + assert.strictEqual(worker.suicide, true); | ||
| 11 | + })); | ||
| 9 | 12 | ||
| 10 | - worker.on('disconnect', common.mustCall(function() { | ||
| 11 | - assert.strictEqual(worker.suicide, true); | ||
| 12 | - disconnected = true; | ||
| 13 | - })); | ||
| 13 | + worker.on('exit', common.mustCall(() => { | ||
| 14 | + assert.strictEqual(worker.suicide, true); | ||
| 15 | + })); | ||
| 16 | + } | ||
| 14 | 17 | ||
| 15 | - worker.on('exit', common.mustCall(function() { | ||
| 16 | - assert.strictEqual(worker.suicide, true); | ||
| 17 | - assert.strictEqual(disconnected, true); | ||
| 18 | - })); | ||
| 18 | + forkWorker('disconnect'); | ||
| 19 | + forkWorker('kill'); | ||
| 19 | 20 | } else { | |
| 20 | - cluster.worker.disconnect(); | ||
| 21 | + cluster.worker[process.env.action](); | ||
| 21 | 22 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const cluster = require('cluster'); | ||
| 5 | + const os = require('os'); | ||
| 6 | + | ||
| 7 | + if (cluster.isMaster) { | ||
| 8 | + function forkWorker(action) { | ||
| 9 | + const worker = cluster.fork({ action }); | ||
| 10 | + worker.on('disconnect', common.mustCall(() => { | ||
| 11 | + assert.strictEqual(worker.suicide, true); | ||
| 12 | + })); | ||
| 13 | + | ||
| 14 | + worker.on('exit', common.mustCall(() => { | ||
| 15 | + assert.strictEqual(worker.suicide, true); | ||
| 16 | + })); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + const cpus = os.cpus().length; | ||
| 20 | + const tries = cpus > 8 ? 64 : cpus * 8; | ||
| 21 | + | ||
| 22 | + cluster.on('exit', common.mustCall((worker, code) => { | ||
| 23 | + assert.strictEqual(code, 0, 'worker exited with error'); | ||
| 24 | + }, tries * 2)); | ||
| 25 | + | ||
| 26 | + for (let i = 0; i < tries; ++i) { | ||
| 27 | + forkWorker('disconnect'); | ||
| 28 | + forkWorker('kill'); | ||
| 29 | + } | ||
| 30 | + } else { | ||
| 31 | + cluster.worker[process.env.action](); | ||
| 32 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments