node: enable workers by default. throw warnings if native bindings were not built.

This commit is contained in:
Christopher Jeffrey 2017-01-22 20:39:42 -08:00
parent e32f521a43
commit 86b7292c4a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 47 additions and 6 deletions

View File

@ -20,6 +20,13 @@ var BN = require('bn.js');
var ec = exports;
/**
* Whether we're using native bindings.
* @const {Boolean}
*/
ec.binding = false;
/**
* Generate a private key.
* @returns {Buffer} Private key.

View File

@ -31,6 +31,13 @@ var HALF_ORDER = new Buffer(
var ec = exports;
/**
* Whether we're using native bindings.
* @const {Boolean}
*/
ec.binding = true;
/**
* Generate a private key.
* @returns {Buffer} Private key.

View File

@ -15,6 +15,8 @@ var Network = require('../protocol/network');
var Logger = require('./logger');
var NodeClient = require('./nodeclient');
var workerPool = require('../workers/workerpool').pool;
var ec = require('../crypto/ec');
var native = require('../utils/native');
/**
* Base class from which every other
@ -116,15 +118,14 @@ Node.prototype.init = function init(options) {
this.client = new NodeClient(this);
this.on('preopen', function() {
self.handleOpen();
self.handlePreopen();
});
this.on('open', function() {
self.startTime = util.now();
self.handleOpen();
});
this.on('close', function() {
self.startTime = -1;
self.handleClose();
});
};
@ -134,7 +135,7 @@ Node.prototype.init = function init(options) {
* @private
*/
Node.prototype.handleOpen = function handleOpen() {
Node.prototype.handlePreopen = function handlePreopen() {
var self = this;
this.logger.open();
@ -171,6 +172,30 @@ Node.prototype.handleOpen = function handleOpen() {
});
};
/**
* Open node.
* @private
*/
Node.prototype.handleOpen = function handleOpen() {
this.startTime = util.now();
if (!ec.binding) {
this.logger.warning('Warning: secp256k1-node was not built.');
this.logger.warning('Verification will be slow.');
}
if (!native.binding) {
this.logger.warning('Warning: bcoin-native was not built.');
this.logger.warning('Hashing will be slow.');
}
if (!workerPool.enabled) {
this.logger.warning('Warning: worker pool is disabled.');
this.logger.warning('Verification will be slow.');
}
};
/**
* Close node. Unbind all events.
* @private
@ -179,6 +204,8 @@ Node.prototype.handleOpen = function handleOpen() {
Node.prototype.handleClose = function handleClose() {
var i, bound;
this.startTime = -1;
this.logger.close();
for (i = 0; i < this.bound.length; i++) {

View File

@ -870,7 +870,7 @@ function getCores() {
*/
exports.pool = new WorkerPool();
exports.pool.enabled = false;
exports.pool.enabled = true;
exports.set = function set(options) {
this.pool.set({
@ -881,7 +881,7 @@ exports.set = function set(options) {
};
exports.set({
useWorkers: +process.env.BCOIN_USE_WORKERS === 1,
useWorkers: +process.env.BCOIN_USE_WORKERS !== 0,
maxWorkers: +process.env.BCOIN_MAX_WORKERS,
workerTimeout: +process.env.BCOIN_WORKER_TIMEOUT
});