diff --git a/bin/node b/bin/node index ffe40c05..a440d270 100755 --- a/bin/node +++ b/bin/node @@ -31,6 +31,7 @@ const node = new bcoin.fullnode({ logLevel: 'debug', db: 'leveldb', persistent: true, + workers: true, listen: true, loader: require }); diff --git a/bin/spvnode b/bin/spvnode index 0feb1fc5..000a2eb1 100755 --- a/bin/spvnode +++ b/bin/spvnode @@ -18,6 +18,7 @@ const node = bcoin.spvnode({ logLevel: 'debug', db: 'leveldb', persistent: true, + workers: true, listen: true, loader: require }); diff --git a/lib/workers/child.js b/lib/workers/child.js index e7f65b8e..55ca428e 100644 --- a/lib/workers/child.js +++ b/lib/workers/child.js @@ -28,8 +28,8 @@ function Child(file, env) { EventEmitter.call(this); - children.add(this); bindExit(); + children.add(this); this.init(file, env); } @@ -112,31 +112,43 @@ Child.prototype.destroy = function destroy() { this.child.kill('SIGTERM'); }; -/* - * Helpers +/** + * Cleanup all child processes. + * @private */ function bindExit() { - let onSighup, onSigint, onSigterm, onError; - if (exitBound) return; exitBound = true; - onSighup = () => { + listenExit(() => { + for (let child of children) + child.destroy(); + }); +} + +/** + * Listen for exit. + * @param {Function} handler + * @private + */ + +function listenExit(handler) { + let onSighup = () => { process.exit(1 | 0x80); }; - onSigint = () => { + let onSigint = () => { process.exit(2 | 0x80); }; - onSigterm = () => { + let onSigterm = () => { process.exit(15 | 0x80); }; - onError = (err) => { + let onError = (err) => { if (err && err.stack) console.error(err.stack + ''); else @@ -145,10 +157,7 @@ function bindExit() { process.exit(1); }; - process.once('exit', () => { - for (let child of children) - child.destroy(); - }); + process.once('exit', handler); if (process.listenerCount('SIGHUP') === 0) process.once('SIGHUP', onSighup); diff --git a/lib/workers/jobs.js b/lib/workers/jobs.js index 25891dcf..1a6bf222 100644 --- a/lib/workers/jobs.js +++ b/lib/workers/jobs.js @@ -27,7 +27,7 @@ const jobs = exports; jobs.execute = function execute(p) { try { - return jobs._execute(p); + return jobs.handle(p); } catch (e) { return new packets.ErrorResultPacket(e); } @@ -41,7 +41,7 @@ jobs.execute = function execute(p) { * @throws on unknown command */ -jobs._execute = function execute(p) { +jobs.handle = function handle(p) { switch (p.cmd) { case packets.types.VERIFY: return jobs.verify(p.tx, p.view, p.flags); diff --git a/lib/workers/master.js b/lib/workers/master.js index 9fae03f7..1bc77155 100644 --- a/lib/workers/master.js +++ b/lib/workers/master.js @@ -1,5 +1,5 @@ /*! - * workers.js - worker processes for bcoin + * master.js - master process for bcoin * Copyright (c) 2014-2015, Fedor Indutny (MIT License) * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). * https://github.com/bcoin-org/bcoin diff --git a/lib/workers/workerpool.js b/lib/workers/workerpool.js index 3b5888c1..c702586d 100644 --- a/lib/workers/workerpool.js +++ b/lib/workers/workerpool.js @@ -25,11 +25,11 @@ const packets = require('./packets'); * @constructor * @param {Object} options * @param {Number} [options.size=num-cores] - Max pool size. - * @param {Number} [options.timeout=10000] - Execution timeout. + * @param {Number} [options.timeout=120000] - Execution timeout. * @property {Number} size * @property {Number} timeout * @property {Map} children - * @property {Number} nonce + * @property {Number} uid */ function WorkerPool(options) { @@ -38,13 +38,14 @@ function WorkerPool(options) { EventEmitter.call(this); + this.enabled = false; this.size = getCores(); - this.timeout = 60000; - this.children = new Map(); - this.nonce = 0; - this.enabled = true; + this.timeout = 120000; this.file = process.env.BCOIN_WORKER_FILE || 'worker.js'; + this.children = new Map(); + this.uid = 0; + this.set(options); } @@ -142,7 +143,7 @@ WorkerPool.prototype.spawn = function spawn(id) { */ WorkerPool.prototype.alloc = function alloc() { - let id = this.nonce++ % this.size; + let id = this.uid++ % this.size; if (!this.children.has(id)) this.children.set(id, this.spawn(id)); @@ -191,7 +192,7 @@ WorkerPool.prototype.execute = function execute(packet, timeout) { return new Promise((resolve, reject) => { setImmediate(() => { try { - result = jobs._execute(packet); + result = jobs.handle(packet); } catch (e) { reject(e); return; @@ -590,7 +591,7 @@ function PendingJob(worker, id, resolve, reject) { */ PendingJob.prototype.start = function start(timeout) { - if (!timeout || timeout === -1) + if (!timeout || timeout <= 0) return; this.timer = setTimeout(() => {