From f43084b93a1c048d68f7992357e3116029732ce2 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 13 Dec 2018 20:06:58 +0100 Subject: [PATCH] fix: use retimer to avoid creating so many timers (#289) * use retimer to avoid scheduling so many timers * Fixed linting --- package.json | 4 +++- src/connection/manager.js | 3 ++- src/limit-dialer/queue.js | 8 ++++---- src/observer.js | 2 +- src/stats/stat.js | 9 ++++++--- src/transport.js | 2 ++ test/transports.node.js | 1 + 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 19df229ce1..407a51d2ea 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,8 @@ "once": "^1.4.0", "peer-id": "~0.12.0", "peer-info": "~0.14.1", - "pull-stream": "^3.6.9" + "pull-stream": "^3.6.9", + "retimer": "^2.0.0" }, "contributors": [ "Alan Shaw ", @@ -93,6 +94,7 @@ "Kevin Kwok ", "Kobi Gurkan ", "Maciej Krüger ", + "Matteo Collina ", "Michael Fakhry ", "Oli Evans ", "Pau Ramon Revilla ", diff --git a/src/connection/manager.js b/src/connection/manager.js index 024c187936..571c5c028e 100644 --- a/src/connection/manager.js +++ b/src/connection/manager.js @@ -85,6 +85,7 @@ class ConnectionManager { } conn.getPeerInfo((err, peerInfo) => { + /* eslint no-warning-comments: off */ if (err) { return log('identify not successful') } @@ -140,7 +141,7 @@ class ConnectionManager { encrypt = plaintext.encrypt } - this.switch.crypto = {tag, encrypt} + this.switch.crypto = { tag, encrypt } } /** diff --git a/src/limit-dialer/queue.js b/src/limit-dialer/queue.js index c204b620b2..bd553400c1 100644 --- a/src/limit-dialer/queue.js +++ b/src/limit-dialer/queue.js @@ -42,7 +42,7 @@ class DialQueue { this._dialWithTimeout(transport, addr, (err, conn) => { if (err) { log.error(`${transport.constructor.name}:work`, err) - return callback(null, {error: err}) + return callback(null, { error: err }) } if (token.cancel) { @@ -51,9 +51,9 @@ class DialQueue { pull(pull.empty(), conn) // If we can close the connection, do it if (typeof conn.close === 'function') { - return conn.close((_) => callback(null, {cancel: true})) + return conn.close((_) => callback(null, { cancel: true })) } - return callback(null, {cancel: true}) + return callback(null, { cancel: true }) } // one is enough @@ -99,7 +99,7 @@ class DialQueue { * @returns {void} */ push (transport, addr, token, callback) { - this.queue.push({transport, addr, token}, callback) + this.queue.push({ transport, addr, token }, callback) } } diff --git a/src/observer.js b/src/observer.js index 0504caa7ec..e908757624 100644 --- a/src/observer.js +++ b/src/observer.js @@ -41,7 +41,7 @@ module.exports = (swtch) => { peerInfo.then((_peerInfo) => { if (_peerInfo) { const peerId = _peerInfo.id.toB58String() - setImmediate(() => observer.emit('message', peerId, transport, protocol, direction, bufferLength)) + observer.emit('message', peerId, transport, protocol, direction, bufferLength) } }) } diff --git a/src/stats/stat.js b/src/stats/stat.js index e74463b1f4..a4fff82adb 100644 --- a/src/stats/stat.js +++ b/src/stats/stat.js @@ -3,6 +3,7 @@ const EventEmitter = require('events') const Big = require('big.js').Big const MovingAverage = require('moving-average') +const retimer = require('retimer') /** * A queue based manager for stat processing @@ -55,7 +56,8 @@ class Stats extends EventEmitter { */ stop () { if (this._timeout) { - clearTimeout(this._timeout) + this._timeout.clear() + this._timeout = null } } @@ -98,9 +100,10 @@ class Stats extends EventEmitter { */ _resetComputeTimeout () { if (this._timeout) { - clearTimeout(this._timeout) + this._timeout.reschedule(this._nextTimeout()) + } else { + this._timeout = retimer(this._update, this._nextTimeout()) } - this._timeout = setTimeout(this._update, this._nextTimeout()) } /** diff --git a/src/transport.js b/src/transport.js index 5635531c1a..b8321df1fb 100644 --- a/src/transport.js +++ b/src/transport.js @@ -1,5 +1,7 @@ 'use strict' +/* eslint no-warning-comments: off */ + const parallel = require('async/parallel') const once = require('once') const debug = require('debug') diff --git a/test/transports.node.js b/test/transports.node.js index 98347f1a52..2cfa49e8d6 100644 --- a/test/transports.node.js +++ b/test/transports.node.js @@ -1,4 +1,5 @@ /* eslint-env mocha */ +/* eslint no-warning-comments: off */ 'use strict' const chai = require('chai')