Skip to content

Commit f43084b

Browse files
mcollinajacobheun
authored andcommitted
fix: use retimer to avoid creating so many timers (libp2p#289)
* use retimer to avoid scheduling so many timers * Fixed linting
1 parent 823c776 commit f43084b

File tree

7 files changed

+19
-10
lines changed

7 files changed

+19
-10
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
"once": "^1.4.0",
7676
"peer-id": "~0.12.0",
7777
"peer-info": "~0.14.1",
78-
"pull-stream": "^3.6.9"
78+
"pull-stream": "^3.6.9",
79+
"retimer": "^2.0.0"
7980
},
8081
"contributors": [
8182
"Alan Shaw <[email protected]>",
@@ -93,6 +94,7 @@
9394
"Kevin Kwok <[email protected]>",
9495
"Kobi Gurkan <[email protected]>",
9596
"Maciej Krüger <[email protected]>",
97+
"Matteo Collina <[email protected]>",
9698
"Michael Fakhry <[email protected]>",
9799
"Oli Evans <[email protected]>",
98100
"Pau Ramon Revilla <[email protected]>",

src/connection/manager.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class ConnectionManager {
8585
}
8686

8787
conn.getPeerInfo((err, peerInfo) => {
88+
/* eslint no-warning-comments: off */
8889
if (err) {
8990
return log('identify not successful')
9091
}
@@ -140,7 +141,7 @@ class ConnectionManager {
140141
encrypt = plaintext.encrypt
141142
}
142143

143-
this.switch.crypto = {tag, encrypt}
144+
this.switch.crypto = { tag, encrypt }
144145
}
145146

146147
/**

src/limit-dialer/queue.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DialQueue {
4242
this._dialWithTimeout(transport, addr, (err, conn) => {
4343
if (err) {
4444
log.error(`${transport.constructor.name}:work`, err)
45-
return callback(null, {error: err})
45+
return callback(null, { error: err })
4646
}
4747

4848
if (token.cancel) {
@@ -51,9 +51,9 @@ class DialQueue {
5151
pull(pull.empty(), conn)
5252
// If we can close the connection, do it
5353
if (typeof conn.close === 'function') {
54-
return conn.close((_) => callback(null, {cancel: true}))
54+
return conn.close((_) => callback(null, { cancel: true }))
5555
}
56-
return callback(null, {cancel: true})
56+
return callback(null, { cancel: true })
5757
}
5858

5959
// one is enough
@@ -99,7 +99,7 @@ class DialQueue {
9999
* @returns {void}
100100
*/
101101
push (transport, addr, token, callback) {
102-
this.queue.push({transport, addr, token}, callback)
102+
this.queue.push({ transport, addr, token }, callback)
103103
}
104104
}
105105

src/observer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = (swtch) => {
4141
peerInfo.then((_peerInfo) => {
4242
if (_peerInfo) {
4343
const peerId = _peerInfo.id.toB58String()
44-
setImmediate(() => observer.emit('message', peerId, transport, protocol, direction, bufferLength))
44+
observer.emit('message', peerId, transport, protocol, direction, bufferLength)
4545
}
4646
})
4747
}

src/stats/stat.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const EventEmitter = require('events')
44
const Big = require('big.js').Big
55
const MovingAverage = require('moving-average')
6+
const retimer = require('retimer')
67

78
/**
89
* A queue based manager for stat processing
@@ -55,7 +56,8 @@ class Stats extends EventEmitter {
5556
*/
5657
stop () {
5758
if (this._timeout) {
58-
clearTimeout(this._timeout)
59+
this._timeout.clear()
60+
this._timeout = null
5961
}
6062
}
6163

@@ -98,9 +100,10 @@ class Stats extends EventEmitter {
98100
*/
99101
_resetComputeTimeout () {
100102
if (this._timeout) {
101-
clearTimeout(this._timeout)
103+
this._timeout.reschedule(this._nextTimeout())
104+
} else {
105+
this._timeout = retimer(this._update, this._nextTimeout())
102106
}
103-
this._timeout = setTimeout(this._update, this._nextTimeout())
104107
}
105108

106109
/**

src/transport.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
/* eslint no-warning-comments: off */
4+
35
const parallel = require('async/parallel')
46
const once = require('once')
57
const debug = require('debug')

test/transports.node.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-env mocha */
2+
/* eslint no-warning-comments: off */
23
'use strict'
34

45
const chai = require('chai')

0 commit comments

Comments
 (0)