Skip to content

Commit f731cdc

Browse files
authored
fix: peer disconnect event and improve logging performance (libp2p#309)
* fix: only emit disconnects from muxed conns * fix: update disconnect logic * chore: clean up logging to prevent unneeded string formatting * chore: fix spelling
1 parent 33d49e9 commit f731cdc

File tree

6 files changed

+33
-34
lines changed

6 files changed

+33
-34
lines changed

src/connection/base.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BaseConnection extends EventEmitter {
2222
*/
2323
close (err) {
2424
if (this._state._state === 'DISCONNECTING') return
25-
this.log(`closing connection to ${this.theirB58Id}`)
25+
this.log('closing connection to %s', this.theirB58Id)
2626
if (err && this._events.error) {
2727
this.emit('error', err)
2828
}
@@ -80,7 +80,7 @@ class BaseConnection extends EventEmitter {
8080
* @returns {void}
8181
*/
8282
_onDisconnected () {
83-
this.log(`disconnected from ${this.theirB58Id}`)
83+
this.log('disconnected from %s', this.theirB58Id)
8484
this.emit('close')
8585
this.removeAllListeners()
8686
}
@@ -92,7 +92,7 @@ class BaseConnection extends EventEmitter {
9292
* @returns {void}
9393
*/
9494
_onPrivatized () {
95-
this.log(`successfully privatized incoming connection`)
95+
this.log('successfully privatized incoming connection')
9696
this.emit('private', this.conn)
9797
}
9898

@@ -113,7 +113,7 @@ class BaseConnection extends EventEmitter {
113113
return this.close(err)
114114
}
115115

116-
this.log(`successfully privatized conn to ${this.theirB58Id}`)
116+
this.log('successfully privatized conn to %s', this.theirB58Id)
117117
this.conn.setPeerInfo(this.theirPeerInfo)
118118
this._state('done')
119119
})

src/connection/incoming.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ class IncomingConnectionFSM extends BaseConnection {
5757
this._state.on('PRIVATIZED', () => this._onPrivatized())
5858
this._state.on('ENCRYPTING', () => this._onEncrypting())
5959
this._state.on('ENCRYPTED', () => {
60-
this.log(`successfully encrypted connection to ${this.theirB58Id || 'unknown peer'}`)
60+
this.log('successfully encrypted connection to %s', this.theirB58Id || 'unknown peer')
6161
this.emit('encrypted', this.conn)
6262
})
6363
this._state.on('UPGRADING', () => this._onUpgrading())
6464
this._state.on('MUXED', () => {
65-
this.log(`successfully muxed connection to ${this.theirB58Id || 'unknown peer'}`)
65+
this.log('successfully muxed connection to %s', this.theirB58Id || 'unknown peer')
6666
this.emit('muxed', this.conn)
6767
})
6868
this._state.on('DISCONNECTING', () => {
@@ -81,7 +81,7 @@ class IncomingConnectionFSM extends BaseConnection {
8181
* @returns {void}
8282
*/
8383
_onEncrypting () {
84-
this.log(`encrypting connection via ${this.switch.crypto.tag}`)
84+
this.log('encrypting connection via %s', this.switch.crypto.tag)
8585

8686
this.msListener.addHandler(this.switch.crypto.tag, (protocol, _conn) => {
8787
this.conn = this.switch.crypto.encrypt(this.ourPeerInfo.id, _conn, undefined, (err) => {

src/connection/index.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ class ConnectionFSM extends BaseConnection {
115115
this._state.on('PRIVATIZED', () => this._onPrivatized())
116116
this._state.on('ENCRYPTING', () => this._onEncrypting())
117117
this._state.on('ENCRYPTED', () => {
118-
this.log(`successfully encrypted connection to ${this.theirB58Id}`)
118+
this.log('successfully encrypted connection to %s', this.theirB58Id)
119119
this.emit('encrypted', this.conn)
120120
})
121121
this._state.on('UPGRADING', () => this._onUpgrading())
122122
this._state.on('MUXED', () => {
123-
this.log(`successfully muxed connection to ${this.theirB58Id}`)
123+
this.log('successfully muxed connection to %s', this.theirB58Id)
124124
delete this.switch.conns[this.theirB58Id]
125125
this.emit('muxed', this.muxer)
126126
})
127127
this._state.on('CONNECTED', () => {
128-
this.log(`unmuxed connection opened to ${this.theirB58Id}`)
128+
this.log('unmuxed connection opened to %s', this.theirB58Id)
129129
this.emit('unmuxed', this.conn)
130130
})
131131
this._state.on('DISCONNECTING', () => this._onDisconnecting())
@@ -169,7 +169,7 @@ class ConnectionFSM extends BaseConnection {
169169
return callback(err, null)
170170
}
171171

172-
this.log(`created new stream to ${this.theirB58Id}`)
172+
this.log('created new stream to %s', this.theirB58Id)
173173
this._protocolHandshake(protocol, stream, callback)
174174
})
175175
}
@@ -194,7 +194,7 @@ class ConnectionFSM extends BaseConnection {
194194
* @returns {void}
195195
*/
196196
_onDialing () {
197-
this.log(`dialing ${this.theirB58Id}`)
197+
this.log('dialing %s', this.theirB58Id)
198198

199199
if (!this.switch.hasTransports()) {
200200
return this.close(NO_TRANSPORTS_REGISTERED())
@@ -226,7 +226,7 @@ class ConnectionFSM extends BaseConnection {
226226
this.theirPeerInfo.multiaddrs.add(`/p2p-circuit/p2p/${this.theirB58Id}`)
227227
}
228228

229-
this.log(`dialing transport ${transport}`)
229+
this.log('dialing transport %s', transport)
230230
this.switch.transport.dial(transport, this.theirPeerInfo, (errors, _conn) => {
231231
if (errors) {
232232
this.emit('error:connection_attempt_failed', errors)
@@ -250,7 +250,7 @@ class ConnectionFSM extends BaseConnection {
250250
* @returns {void}
251251
*/
252252
_onDialed () {
253-
this.log(`successfully dialed ${this.theirB58Id}`)
253+
this.log('successfully dialed %s', this.theirB58Id)
254254

255255
this.emit('connected', this.conn)
256256
}
@@ -261,33 +261,32 @@ class ConnectionFSM extends BaseConnection {
261261
* @returns {void}
262262
*/
263263
_onDisconnecting () {
264-
this.log(`disconnecting from ${this.theirB58Id}`)
264+
this.log('disconnecting from %s', this.theirB58Id)
265265

266266
// Issue disconnects on both Peers
267267
if (this.theirPeerInfo) {
268268
this.theirPeerInfo.disconnect()
269269
}
270270

271+
this.switch.connection.remove(this)
272+
273+
delete this.switch.conns[this.theirB58Id]
274+
271275
// Clean up stored connections
272276
if (this.muxer) {
273277
this.muxer.end()
278+
delete this.muxer
279+
this.switch.emit('peer-mux-closed', this.theirPeerInfo)
274280
}
275281

276-
this.switch.connection.remove(this)
277-
278-
delete this.switch.conns[this.theirB58Id]
279-
delete this.muxer
280-
281282
// If we have the base connection, abort it
282283
if (this.conn) {
283284
this.conn.source(true, () => {
284285
this._state('done')
285-
this.switch.emit('peer-mux-closed', this.theirPeerInfo)
286286
delete this.conn
287287
})
288288
} else {
289289
this._state('done')
290-
this.switch.emit('peer-mux-closed', this.theirPeerInfo)
291290
}
292291
}
293292

@@ -336,7 +335,7 @@ class ConnectionFSM extends BaseConnection {
336335
*/
337336
_onUpgrading () {
338337
const muxers = Object.keys(this.switch.muxers)
339-
this.log(`upgrading connection to ${this.theirB58Id}`)
338+
this.log('upgrading connection to %s', this.theirB58Id)
340339

341340
if (muxers.length === 0) {
342341
return this._state('stop')
@@ -376,7 +375,7 @@ class ConnectionFSM extends BaseConnection {
376375

377376
// For incoming streams, in case identify is on
378377
this.muxer.on('stream', (conn) => {
379-
this.log(`new stream created via muxer to ${this.theirB58Id}`)
378+
this.log('new stream created via muxer to %s', this.theirB58Id)
380379
conn.setPeerInfo(this.theirPeerInfo)
381380
this.switch.protocolMuxer(null)(conn)
382381
})
@@ -431,12 +430,12 @@ class ConnectionFSM extends BaseConnection {
431430

432431
msDialer.select(protocol, (err, _conn) => {
433432
if (err) {
434-
this.log(`could not perform protocol handshake: `, err)
433+
this.log('could not perform protocol handshake:', err)
435434
return callback(err, null)
436435
}
437436

438437
const conn = observeConnection(null, protocol, _conn, this.switch.observer)
439-
this.log(`successfully performed handshake of ${protocol} to ${this.theirB58Id}`)
438+
this.log('successfully performed handshake of %s to %s', protocol, this.theirB58Id)
440439
this.emit('connection', conn)
441440
callback(null, conn)
442441
})

src/dialer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Connection = require('interface-connection').Connection
44
const ConnectionFSM = require('./connection')
55
const getPeerInfo = require('./get-peer-info')
66
const once = require('once')
7-
const setImmediate = require('async/setImmediate')
7+
const nextTick = require('async/nextTick')
88

99
const debug = require('debug')
1010
const log = debug('libp2p:switch:dial')
@@ -22,7 +22,7 @@ function maybePerformHandshake ({ protocol, proxyConnection, connection, callbac
2222
})
2323
}
2424

25-
callback()
25+
nextTick(callback)
2626
}
2727

2828
/**
@@ -53,7 +53,7 @@ function dial (_switch, returnFSM) {
5353
const peerInfo = getPeerInfo(peer, _switch._peerBook)
5454
const b58Id = peerInfo.id.toB58String()
5555

56-
log(`dialing to ${b58Id.slice(0, 8)} with protocol ${protocol || 'unknown'}`)
56+
log('dialing to %s with protocol %s', b58Id, protocol || 'unknown')
5757

5858
let connection = _switch.connection.getOne(b58Id)
5959

@@ -89,7 +89,7 @@ function dial (_switch, returnFSM) {
8989
const proxyConnection = new Connection()
9090
proxyConnection.setPeerInfo(peerInfo)
9191

92-
setImmediate(() => {
92+
nextTick(() => {
9393
// If we have a muxed connection, attempt the protocol handshake
9494
if (connection.getState() === 'MUXED') {
9595
maybePerformHandshake({

src/limit-dialer/queue.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class DialQueue {
4141
*/
4242
_doWork (transport, addr, token, callback) {
4343
callback = once(callback)
44-
log(`${transport.constructor.name}:work:start`)
44+
log('work:start')
4545
this._dialWithTimeout(transport, addr, (err, conn) => {
4646
if (err) {
4747
log.error(`${transport.constructor.name}:work`, err)
4848
return callback(err)
4949
}
5050

5151
if (token.cancel) {
52-
log(`${transport.constructor.name}:work:cancel`)
52+
log('work:cancel')
5353
// clean up already done dials
5454
pull(empty(), conn)
5555
// If we can close the connection, do it
@@ -62,7 +62,7 @@ class DialQueue {
6262
// one is enough
6363
token.cancel = true
6464

65-
log(`${transport.constructor.name}:work:success`)
65+
log('work:success')
6666

6767
const proxyConn = new Connection()
6868
proxyConn.setInnerConn(conn)

src/protocol-muxer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = function protocolMuxer (protocols, observer) {
2525
}
2626

2727
const handler = (protocolName, _conn) => {
28-
log(`registering handler with protocol ${protocolName}`)
28+
log('registering handler with protocol %s', protocolName)
2929
const protocol = protocols[protocolName]
3030
if (protocol) {
3131
const handlerFunc = protocol && protocol.handlerFunc

0 commit comments

Comments
 (0)