workers: make worker files configurable.
This commit is contained in:
parent
8f295a376d
commit
4ebe557807
@ -235,6 +235,8 @@ node = new bcoin.fullnode({
|
|||||||
coinCache: 30000000,
|
coinCache: 30000000,
|
||||||
logConsole: true,
|
logConsole: true,
|
||||||
workers: true,
|
workers: true,
|
||||||
|
workerURL: '/bcoin-worker.js',
|
||||||
|
masterURL: '/bcoin-master.js',
|
||||||
logger: logger
|
logger: logger
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -88,7 +88,10 @@ Node.prototype.initOptions = function initOptions() {
|
|||||||
this.workers = new WorkerPool({
|
this.workers = new WorkerPool({
|
||||||
enabled: config.bool('workers'),
|
enabled: config.bool('workers'),
|
||||||
size: config.num('workers-size'),
|
size: config.num('workers-size'),
|
||||||
timeout: config.num('workers-timeout')
|
timeout: config.num('workers-timeout'),
|
||||||
|
workerFile: config.str('worker-file'),
|
||||||
|
workerURL: config.str('worker-url'),
|
||||||
|
masterURL: config.str('master-url')
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -84,7 +84,9 @@ Master.prototype._initWebWorkers = function _initWebWorkers() {
|
|||||||
global.onmessage = (event) => {
|
global.onmessage = (event) => {
|
||||||
let data;
|
let data;
|
||||||
if (typeof event.data !== 'string') {
|
if (typeof event.data !== 'string') {
|
||||||
data = event.data.buf;
|
assert(event.data && typeof event.data === 'object');
|
||||||
|
assert(event.data.data);
|
||||||
|
data = event.data.data;
|
||||||
data.__proto__ = Buffer.prototype;
|
data.__proto__ = Buffer.prototype;
|
||||||
} else {
|
} else {
|
||||||
data = Buffer.from(event.data, 'hex');
|
data = Buffer.from(event.data, 'hex');
|
||||||
@ -110,7 +112,7 @@ Master.prototype._initChildProcess = function _initChildProcess() {
|
|||||||
|
|
||||||
process.on('uncaughtException', (err) => {
|
process.on('uncaughtException', (err) => {
|
||||||
this.send(new packets.ErrorPacket(err));
|
this.send(new packets.ErrorPacket(err));
|
||||||
setImmediate(() => process.exit(1));
|
setTimeout(() => process.exit(1), 500);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -133,7 +135,7 @@ Master.prototype.write = function write(data) {
|
|||||||
if (HAS_WORKERS) {
|
if (HAS_WORKERS) {
|
||||||
if (global.postMessage.length === 2) {
|
if (global.postMessage.length === 2) {
|
||||||
data.__proto__ = Uint8Array.prototype;
|
data.__proto__ = Uint8Array.prototype;
|
||||||
global.postMessage({ buf: data }, [data]);
|
global.postMessage({ data }, [data]);
|
||||||
} else {
|
} else {
|
||||||
global.postMessage(data.toString('hex'));
|
global.postMessage(data.toString('hex'));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,6 +52,10 @@ function WorkerPool(options) {
|
|||||||
this.nonce = 0;
|
this.nonce = 0;
|
||||||
this.enabled = true;
|
this.enabled = true;
|
||||||
|
|
||||||
|
this.workerFile = WorkerPool.WORKER_FILE;
|
||||||
|
this.workerURL = WorkerPool.WORKER_URL;
|
||||||
|
this.masterURL = WorkerPool.MASTER_URL;
|
||||||
|
|
||||||
bindExit();
|
bindExit();
|
||||||
pools.add(this);
|
pools.add(this);
|
||||||
|
|
||||||
@ -72,21 +76,21 @@ WorkerPool.CORES = getCores();
|
|||||||
* @const {String}
|
* @const {String}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WorkerPool.WORKER_FILE = 'worker.js';
|
WorkerPool.WORKER_FILE = process.env.BCOIN_WORKER_FILE || 'worker.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default worker URL.
|
* Default worker URL.
|
||||||
* @const {String}
|
* @const {String}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WorkerPool.WORKER_URL = '/bcoin-worker.js';
|
WorkerPool.WORKER_URL = process.env.BCOIN_WORKER_URL || '/bcoin-worker.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default master URL.
|
* Default master URL.
|
||||||
* @const {String}
|
* @const {String}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
WorkerPool.MASTER_URL = '/bcoin-master.js';
|
WorkerPool.MASTER_URL = process.env.BCOIN_MASTER_URL || '/bcoin-master.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set worker pool options.
|
* Set worker pool options.
|
||||||
@ -113,6 +117,21 @@ WorkerPool.prototype.set = function set(options) {
|
|||||||
assert(options.timeout > 0);
|
assert(options.timeout > 0);
|
||||||
this.timeout = options.timeout;
|
this.timeout = options.timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.workerFile != null) {
|
||||||
|
assert(typeof options.workerFile === 'string');
|
||||||
|
this.workerFile = options.workerFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.workerURL != null) {
|
||||||
|
assert(typeof options.workerURL === 'string');
|
||||||
|
this.workerURL = options.workerURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.masterURL != null) {
|
||||||
|
assert(typeof options.masterURL === 'string');
|
||||||
|
this.masterURL = options.masterURL;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -414,7 +433,7 @@ function Worker(id) {
|
|||||||
this.pending = new Map();
|
this.pending = new Map();
|
||||||
|
|
||||||
this.env = {
|
this.env = {
|
||||||
BCOIN_MASTER_URL: process.env.BCOIN_MASTER_URL || WorkerPool.MASTER_URL,
|
BCOIN_MASTER_URL: this.masterURL,
|
||||||
BCOIN_WORKER_NETWORK: Network.type,
|
BCOIN_WORKER_NETWORK: Network.type,
|
||||||
BCOIN_WORKER_ISTTY: process.stdout
|
BCOIN_WORKER_ISTTY: process.stdout
|
||||||
? (process.stdout.isTTY ? '1' : '0')
|
? (process.stdout.isTTY ? '1' : '0')
|
||||||
@ -463,12 +482,7 @@ Worker.prototype._init = function _init() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Worker.prototype._initWebWorkers = function _initWebWorkers() {
|
Worker.prototype._initWebWorkers = function _initWebWorkers() {
|
||||||
let file = WorkerPool.WORKER_URL;
|
this.child = new global.Worker(this.workerURL);
|
||||||
|
|
||||||
if (process.env.BCOIN_WORKER_URL)
|
|
||||||
file = process.env.BCOIN_WORKER_URL;
|
|
||||||
|
|
||||||
this.child = new global.Worker(file);
|
|
||||||
|
|
||||||
this.child.onerror = (err) => {
|
this.child.onerror = (err) => {
|
||||||
this.emit('error', err);
|
this.emit('error', err);
|
||||||
@ -478,7 +492,9 @@ Worker.prototype._initWebWorkers = function _initWebWorkers() {
|
|||||||
this.child.onmessage = (event) => {
|
this.child.onmessage = (event) => {
|
||||||
let data;
|
let data;
|
||||||
if (typeof event.data !== 'string') {
|
if (typeof event.data !== 'string') {
|
||||||
data = event.data.buf;
|
assert(event.data && typeof event.data === 'object');
|
||||||
|
assert(event.data.data);
|
||||||
|
data = event.data.data;
|
||||||
data.__proto__ = Buffer.prototype;
|
data.__proto__ = Buffer.prototype;
|
||||||
} else {
|
} else {
|
||||||
data = Buffer.from(event.data, 'hex');
|
data = Buffer.from(event.data, 'hex');
|
||||||
@ -496,15 +512,10 @@ Worker.prototype._initWebWorkers = function _initWebWorkers() {
|
|||||||
|
|
||||||
Worker.prototype._initChildProcess = function _initChildProcess() {
|
Worker.prototype._initChildProcess = function _initChildProcess() {
|
||||||
let bin = process.argv[0];
|
let bin = process.argv[0];
|
||||||
let file = WorkerPool.WORKER_FILE;
|
let file = path.resolve(__dirname, this.workerFile);
|
||||||
let env = Object.assign({}, process.env, this.env);
|
let env = Object.assign({}, process.env, this.env);
|
||||||
let options = { stdio: 'pipe', env: env };
|
let options = { stdio: 'pipe', env: env };
|
||||||
|
|
||||||
if (process.env.BCOIN_WORKER_FILE)
|
|
||||||
file = process.env.BCOIN_WORKER_FILE;
|
|
||||||
|
|
||||||
file = path.resolve(__dirname, file);
|
|
||||||
|
|
||||||
this.child = cp.spawn(bin, [file], options);
|
this.child = cp.spawn(bin, [file], options);
|
||||||
|
|
||||||
this.child.unref();
|
this.child.unref();
|
||||||
@ -601,7 +612,7 @@ Worker.prototype.write = function write(data) {
|
|||||||
if (HAS_WORKERS) {
|
if (HAS_WORKERS) {
|
||||||
if (this.child.postMessage.length === 2) {
|
if (this.child.postMessage.length === 2) {
|
||||||
data.__proto__ = Uint8Array.prototype;
|
data.__proto__ = Uint8Array.prototype;
|
||||||
this.child.postMessage({ buf: data }, [data]);
|
this.child.postMessage({ data }, [data]);
|
||||||
} else {
|
} else {
|
||||||
this.child.postMessage(data.toString('hex'));
|
this.child.postMessage(data.toString('hex'));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user