Merge pull request #193 from cubedro/develop

Added debounce strategy for chain download
This commit is contained in:
Marian OANCΞA 2015-06-17 23:05:58 +03:00
commit da6d089be2
1 changed files with 31 additions and 6 deletions

View File

@ -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);
}
});