From e8f5be9830c8db4ae955de09b924316442339d63 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 22 May 2016 18:13:54 -0700 Subject: [PATCH] workers. fix inv relaying. --- lib/bcoin/miner.js | 3 +++ lib/bcoin/peer.js | 4 ++-- lib/bcoin/workers.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 53c52fb6..77388fda 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -218,6 +218,9 @@ Miner.prototype.stop = function stop() { this.attempt.destroy(); this.attempt = null; } + + if (this.workerPool) + this.workerPool.destroy(); }; /** diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 23c724ab..a6a5fbdd 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -1098,8 +1098,8 @@ Peer.prototype._handleGetData = function handleGetData(items) { return; utils.forEachSerial(check, function(item, next) { - var witness = item.type & constants.WITNESS_MASK; - var type = (item.type & ~constants.WITNESS_MASK) !== 0; + var type = item.type & ~constants.WITNESS_MASK; + var witness = (item.type & constants.WITNESS_MASK) !== 0; var hash = item.hash; var i, tx, data; diff --git a/lib/bcoin/workers.js b/lib/bcoin/workers.js index 7caebaf5..c3fa10fd 100644 --- a/lib/bcoin/workers.js +++ b/lib/bcoin/workers.js @@ -50,6 +50,24 @@ utils.inherits(Workers, EventEmitter); Workers.CORES = getCores(); +/** + * Global list of workers. + * @type {Array} + */ + +Workers.children = []; + +/** + * Cleanup all workers on exit. + */ + +Workers.cleanup = function cleanup() { + while (Workers.children.length > 0) + Workers.children.pop().destroy(); +}; + +Workers._exitBound = false; + /** * Spawn a new worker. * @param {Number} id - Worker ID. @@ -58,7 +76,7 @@ Workers.CORES = getCores(); Workers.prototype.spawn = function spawn(id) { var self = this; - var child; + var i, child; bcoin.debug('Spawning worker process: %d', id); @@ -72,6 +90,9 @@ Workers.prototype.spawn = function spawn(id) { bcoin.debug('Worker %d exited: %s', child.id, code); if (self.children[child.id] === child) self.children[child.id] = null; + i = Workers.children.indexOf(child); + if (i !== -1) + Workers.children.splice(i, 1); }); child.on('packet', function(job, body) { @@ -87,6 +108,13 @@ Workers.prototype.spawn = function spawn(id) { self.emit('error', new Error('Unknown packet: ' + body.name)); }); + Workers.children.push(child); + + if (!Workers._exitBound) { + process.once('exit', Workers.cleanup); + Workers._exitBound = true; + } + return child; };