Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
fix: use retimer to avoid creating so many timers (#289)
Browse files Browse the repository at this point in the history
* use retimer to avoid scheduling so many timers

* Fixed linting
  • Loading branch information
mcollina authored and jacobheun committed Dec 13, 2018
1 parent 823c776 commit f43084b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <alan@tableflip.io>",
Expand All @@ -93,6 +94,7 @@
"Kevin Kwok <antimatter15@gmail.com>",
"Kobi Gurkan <kobigurk@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Matteo Collina <hello@matteocollina.com>",
"Michael Fakhry <fakhrimichael@live.com>",
"Oli Evans <oli@tableflip.io>",
"Pau Ramon Revilla <masylum@gmail.com>",
Expand Down
3 changes: 2 additions & 1 deletion src/connection/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class ConnectionManager {
}

conn.getPeerInfo((err, peerInfo) => {
/* eslint no-warning-comments: off */
if (err) {
return log('identify not successful')
}
Expand Down Expand Up @@ -140,7 +141,7 @@ class ConnectionManager {
encrypt = plaintext.encrypt
}

this.switch.crypto = {tag, encrypt}
this.switch.crypto = { tag, encrypt }
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/limit-dialer/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down
9 changes: 6 additions & 3 deletions src/stats/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,7 +56,8 @@ class Stats extends EventEmitter {
*/
stop () {
if (this._timeout) {
clearTimeout(this._timeout)
this._timeout.clear()
this._timeout = null
}
}

Expand Down Expand Up @@ -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())
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/transport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

/* eslint no-warning-comments: off */

const parallel = require('async/parallel')
const once = require('once')
const debug = require('debug')
Expand Down
1 change: 1 addition & 0 deletions test/transports.node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env mocha */
/* eslint no-warning-comments: off */
'use strict'

const chai = require('chai')
Expand Down

0 comments on commit f43084b

Please sign in to comment.