From 6c319b555b81c8a7023518a72fdbc769b1b3d463 Mon Sep 17 00:00:00 2001 From: Karel Bilek Date: Thu, 23 Mar 2017 00:13:50 +0100 Subject: [PATCH] Adding queue to RPC calls --- lib/index.js | 20 ++++++++++++++++++++ package.json | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 7beb313..4f84470 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,7 @@ var http = require('http'); var https = require('https'); +var async = require('async'); function RpcClient(opts) { opts = opts || {}; @@ -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; @@ -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); @@ -40,6 +45,21 @@ 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'); diff --git a/package.json b/package.json index 7005fa6..094873b 100644 --- a/package.json +++ b/package.json @@ -39,13 +39,15 @@ "coverage": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- --recursive" }, "devDependencies": { - "async": "^0.9.0", "chai": "^1.10.0", "coveralls": "^2.11.2", "istanbul": "^0.3.5", "mocha": "^2.1.0", "sinon": "^1.12.2" }, + "dependencies": { + "async": "^1.3.0" + }, "bugs": { "url": "https://github.com/bitpay/bitcoind-rpc/issues" },