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

fix: improve connection tracking #318

Merged
merged 2 commits into from
Apr 3, 2019
Merged

fix: improve connection tracking #318

merged 2 commits into from
Apr 3, 2019

Conversation

jacobheun
Copy link
Contributor

@jacobheun jacobheun commented Apr 2, 2019

Previously, peer-mux-established and peer-muxed-closed events were being fired anytime a connection was created or closed. The problem with this is that if you happened to have 2 connections to a peer and only 1 of them closed, the closed event would fire, causing the connection manager to believe that peer no longer had any connections. This would result in the connection manager believing there were way fewer connections than actually existed.

This PR moves the emit events and PeerInfo.disconnect calls into the switch.connection class. When connections are added and removed it will check to see if there are any additional connections to that peer. This way, we only fire events when changes actually occur.

@ghost ghost assigned jacobheun Apr 2, 2019
@ghost ghost added the in progress label Apr 2, 2019
@@ -68,9 +68,6 @@ class IncomingConnectionFSM extends BaseConnection {
this.emit('muxed', this.conn)
})
this._state.on('DISCONNECTING', () => {
if (this.theirPeerInfo) {
this.theirPeerInfo.disconnect()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unnecessary. Incoming connections don't cause the connect to happen until after the are muxed, which will now be handled by the switch.connection.add() logic.

@@ -325,13 +319,13 @@ class ConnectionFSM extends BaseConnection {
return this.close(maybeUnexpectedEnd(err))
}

const conn = observeConnection(null, this.switch.crypto.tag, _conn, this.switch.observer)

this.conn = this.switch.crypto.encrypt(this.ourPeerInfo.id, conn, this.theirPeerInfo.id, (err) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an edge case here, where if the connection was terminated while encryption was being negotiated, this.conn would get resolved and would later throw an error when secio went to set the inner connection. This avoids that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

proxyConnection.setInnerConn(conn)
callback(null, proxyConnection)
conn.setPeerInfo(connection.theirPeerInfo)
callback(null, conn)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proxyConnection was superfluous here, so I got rid of it.

@jacobheun jacobheun marked this pull request as ready for review April 2, 2019 16:31
@jacobheun jacobheun changed the title [WIP] fix: improve connection tracking fix: improve connection tracking Apr 2, 2019
connection.theirPeerInfo.disconnect()
this.switch.emit('peer-mux-closed', connection.theirPeerInfo)
return
}

for (let i = 0; i < this.connections[connection.theirB58Id].length; i++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the code in this class could be simpler with Set and Map - suggestion for a future PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, a Map would allow for handling the two dimensional array pretty cleanly.

@dirkmc
Copy link

dirkmc commented Apr 2, 2019

I left a couple of questions in the code but in general it seems like a much more reliable way of doing things to me 👍

@dirkmc
Copy link

dirkmc commented Apr 2, 2019

LGTM 👍

Copy link
Member

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Good to know that the inconsistency between the number of peers in the connection manager and in reality should be fixed.

@jacobheun jacobheun merged commit 828e685 into master Apr 3, 2019
@jacobheun jacobheun deleted the fix/conn-tracking branch April 3, 2019 09:35
@ghost ghost removed the in progress label Apr 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants