From 24ba9f503043e590d72d670894e67039d776a912 Mon Sep 17 00:00:00 2001 From: cubedro Date: Wed, 17 Jun 2015 23:05:43 +0300 Subject: [PATCH] added debounce strategy for chain download --- lib/node.js | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/node.js b/lib/node.js index 5b4a691..9b717ca 100644 --- a/lib/node.js +++ b/lib/node.js @@ -114,10 +114,14 @@ function Node () this.pingInterval = false; this.connectionInterval = false; + this._lastBlockSentAt = 0; this._lastChainLog = 0; this._lastPendingLog = 0; this._chainDebouncer = 0; - this._connection_attempts = 0 + this._chan_min_time = 50; + this._max_chain_debouncer = 20; + this._chain_debouncer_cnt = 0; + this._connection_attempts = 0; this._timeOffset = null; this.startWeb3Connection(); @@ -651,6 +655,7 @@ Node.prototype.prepareStats = function () Node.prototype.sendBlockUpdate = function() { + this._lastBlockSentAt = _.now(); console.info("wsc", "Sending", chalk.reset.red("block"), chalk.bold.white("update")); this.emit('block', this.prepareBlock()); } @@ -718,23 +723,43 @@ Node.prototype.setWatches = function() console.info('>>>', 'Chain Filter triggered: ', chalk.reset.red(hash), '- last trigger:', chalk.reset.cyan(time)); - if(time < 50) + if(time < self._chan_min_time) { self._chainDebouncer++; - } else { + self._chain_debouncer_cnt++; + + if(self._chain_debouncer_cnt > 100) + { + self._chan_min_time = Math.max(self._chan_min_time + 1, 200); + self._max_chain_debouncer = Math.max(self._max_chain_debouncer - 1, 5); + } + } + else + { + if(time > 5000) + { + self._chan_min_time = 50; + self._max_chain_debouncer = 20; + self._chain_debouncer_cnt = 0; + } + // reset local chain debouncer self._chainDebouncer = 0; } - if(self._chainDebouncer < 10) + if(self._chainDebouncer < self._max_chain_debouncer || now - self._lastBlockSentAt > 5000) { + if(now - self._lastBlockSentAt > 5000) + { + self._lastBlockSentAt = now; + } + self._latestQueue.push(hash); } else { debounce(function() { self._latestQueue.push(hash); - self._chainDebouncer = 0; - }, 50); + }, self._chan_min_time); } });