From ead5ddb81745b0fe7f0c605cedad070ddd3b5fc9 Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Tue, 7 Nov 2017 15:58:54 -0500 Subject: [PATCH] Adding insight request queue --- lib/blockchainexplorers/insight.js | 22 ++++++++++++---------- lib/common/defaults.js | 20 +++++++++++--------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/blockchainexplorers/insight.js b/lib/blockchainexplorers/insight.js index 8e9a75f..667599a 100644 --- a/lib/blockchainexplorers/insight.js +++ b/lib/blockchainexplorers/insight.js @@ -1,6 +1,7 @@ 'use strict'; var _ = require('lodash'); +var async = require('async'); var $ = require('preconditions').singleton(); var log = require('npmlog'); log.debug = log.verbose; @@ -22,7 +23,8 @@ function Insight(opts) { this.network = opts.network || 'livenet'; this.hosts = opts.url; this.userAgent = opts.userAgent || 'bws'; -}; + this.requestQueue = async.queue(this._doRequest.bind(this), Defaults.INSIGHT_REQUEST_POOL_SIZE); +} var _parseErr = function(err, res) { @@ -43,7 +45,7 @@ Insight.prototype._doRequest = function(args, cb) { }; var s = JSON.stringify(args); - if ( s.length > 100 ) + if ( s.length > 100 ) s= s.substr(0,100) + '...'; log.debug('', 'Insight Q: %s', s); requestList(_.defaults(args, opts), cb); @@ -66,7 +68,7 @@ Insight.prototype.getUtxos = function(addresses, cb) { }, }; - this._doRequest(args, function(err, res, unspent) { + this.requestQueue.push(args, function(err, res, unspent) { if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); return cb(null, unspent); }); @@ -84,7 +86,7 @@ Insight.prototype.broadcast = function(rawTx, cb) { }, }; - this._doRequest(args, function(err, res, body) { + this.requestQueue.push(args, function(err, res, body) { if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); return cb(null, body ? body.txid : null); }); @@ -97,7 +99,7 @@ Insight.prototype.getTransaction = function(txid, cb) { json: true, }; - this._doRequest(args, function(err, res, tx) { + this.requestQueue.push(args, function(err, res, tx) { if (res && res.statusCode == 404) return cb(); if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); @@ -127,7 +129,7 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) { }; - this._doRequest(args, function(err, res, txs) { + this.requestQueue.push(args, function(err, res, txs) { if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); if (_.isObject(txs)) { @@ -154,7 +156,7 @@ Insight.prototype.getAddressActivity = function(address, cb) { json: true, }; - this._doRequest(args, function(err, res, result) { + this.requestQueue.push(args, function(err, res, result) { if (res && res.statusCode == 404) return cb(); if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); @@ -175,7 +177,7 @@ Insight.prototype.estimateFee = function(nbBlocks, cb) { path: path, json: true, }; - this._doRequest(args, function(err, res, body) { + this.requestQueue.push(args, function(err, res, body) { if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); return cb(null, body); }); @@ -189,7 +191,7 @@ Insight.prototype.getBlockchainHeight = function(cb) { path: path, json: true, }; - this._doRequest(args, function(err, res, body) { + this.requestQueue.push(args, function(err, res, body) { if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); return cb(null, body.blockChainHeight); }); @@ -204,7 +206,7 @@ Insight.prototype.getTxidsInBlock = function(blockHash, cb) { json: true, }; - this._doRequest(args, function(err, res, body) { + this.requestQueue.push(args, function(err, res, body) { if (err || res.statusCode !== 200) return cb(_parseErr(err, res)); return cb(null, body.tx); }); diff --git a/lib/common/defaults.js b/lib/common/defaults.js index ff14564..d386345 100644 --- a/lib/common/defaults.js +++ b/lib/common/defaults.js @@ -89,7 +89,7 @@ Defaults.UTXO_SELECTION_MAX_FEE_VS_SINGLE_UTXO_FEE_FACTOR = 5; // Minimum allowed amount for tx outputs (including change) in SAT Defaults.MIN_OUTPUT_AMOUNT = 5000; -// Number of confirmations from which tx in history will be cached +// Number of confirmations from which tx in history will be cached // (ie we consider them inmutables) Defaults.CONFIRMATIONS_TO_START_CACHING = 6 * 6; // ~ 6hrs @@ -114,27 +114,29 @@ Defaults.SESSION_EXPIRATION = 1 * 60 * 60; // 1 hour to session expiration Defaults.RateLimit = { createWallet: { - windowMs: 60 * 60 * 1000, // hour window - delayAfter: 8, // begin slowing down responses after the 3rd request - delayMs: 3000, // slow down subsequent responses by 3 seconds per request + windowMs: 60 * 60 * 1000, // hour window + delayAfter: 8, // begin slowing down responses after the 3rd request + delayMs: 3000, // slow down subsequent responses by 3 seconds per request max: 15, // start blocking after 20 request message: "Too many wallets created from this IP, please try again after an hour" }, estimateFee: { windowMs: 60 * 10 *1000, // 10 min window - delayAfter: 5, // begin slowing down responses after the 3rd request - delayMs: 300, // slow down subsequent responses by 3 seconds per request - + delayAfter: 5, // begin slowing down responses after the 3rd request + delayMs: 300, // slow down subsequent responses by 3 seconds per request + max: 10, // start blocking after 200 request message: "Too many request" }, - + // otherPosts: { - // windowMs: 60 * 60 * 1000, // 1 hour window + // windowMs: 60 * 60 * 1000, // 1 hour window // max: 1200 , // 1 post every 3 sec average, max. // }, }; Defaults.COIN = 'btc'; +Defaults.INSIGHT_REQUEST_POOL_SIZE = 20; + module.exports = Defaults;