Skip to content

Commit

Permalink
rpc queue for meritd.js
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-sy committed Jul 9, 2018
1 parent 4dd746f commit 1b827b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/bitcore-node/lib/services/meritd.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,8 @@ Merit.prototype._spawnChildProcess = function(callback) {
host: '127.0.0.1',
port: self.spawn.config.rpcport,
user: self.spawn.config.rpcuser,
pass: self.spawn.config.rpcpassword
pass: self.spawn.config.rpcpassword,
queue: self.spawn.config.rpcqueue,
});

self._loadTipFromNode(node, done);
Expand Down Expand Up @@ -1051,7 +1052,8 @@ Merit.prototype._connectProcess = function(config, callback) {
port: config.rpcport,
user: config.rpcuser,
pass: config.rpcpassword,
rejectUnauthorized: _.isUndefined(config.rpcstrict) ? true : config.rpcstrict
rejectUnauthorized: _.isUndefined(config.rpcstrict) ? true : config.rpcstrict,
queue: config.rpcqueue,
});

self._loadTipFromNode(node, done);
Expand Down
18 changes: 18 additions & 0 deletions packages/merit-rpc/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var http = require('http');
var https = require('https');
var async = require('async');

function RpcClient(opts) {
opts = opts || {};
Expand All @@ -12,6 +13,7 @@ function RpcClient(opts) {
this.protocol = opts.protocol === 'http' ? http : https;
this.batchedCalls = null;
this.disableAgent = opts.disableAgent || false;
var queueSize = opts.queue || 16;

var isRejectUnauthorized = typeof opts.rejectUnauthorized !== 'undefined';
this.rejectUnauthorized = isRejectUnauthorized ? opts.rejectUnauthorized : true;
Expand All @@ -22,6 +24,9 @@ function RpcClient(opts) {
this.log = RpcClient.loggers[RpcClient.config.logger || 'normal'];
}

this.queue = async.queue(function(task, callback) {
task(callback);
}, queueSize);
}

var cl = console.log.bind(console);
Expand All @@ -39,7 +44,20 @@ RpcClient.config = {
};

function rpc(request, callback) {
var self = this;

var task = function(taskCallback) {
var newCallback = function() {
callback.apply(undefined, arguments);
taskCallback();
};
innerRpc.call(self, request, newCallback);
};

this.queue.push(task);
}

function innerRpc(request, callback) {
var self = this;
request = JSON.stringify(request);
var auth = new Buffer(self.user + ':' + self.pass).toString('base64');
Expand Down

0 comments on commit 1b827b9

Please sign in to comment.