Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: registrar should filter the disconnected conn #532

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Registrar {
let storedConn = this.connections.get(id)

if (storedConn && storedConn.length > 1) {
storedConn = storedConn.filter((conn) => conn.id === connection.id)
storedConn = storedConn.filter((conn) => conn.id !== connection.id)
this.connections.set(id, storedConn)
} else if (storedConn) {
for (const [, topology] of this.topologies) {
Expand Down
40 changes: 40 additions & 0 deletions test/registrar/registrar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Topology = require('libp2p-interfaces/src/topology/multicodec-topology')
const PeerStore = require('../../src/peer-store')
const Registrar = require('../../src/registrar')
const { createMockConnection } = require('./utils')
const peerUtils = require('../utils/creators/peer')

const multicodec = '/test/1.0.0'

Expand Down Expand Up @@ -170,5 +171,44 @@ describe('registrar', () => {

await onDisconnectDefer.promise
})

it('should filter connections on disconnect, removing the closed one', async () => {
const onDisconnectDefer = pDefer()

const topologyProps = new Topology({
multicodecs: multicodec,
handlers: {
onConnect: () => {},
onDisconnect: () => {
onDisconnectDefer.resolve()
}
}
})

// Register protocol
registrar.register(topologyProps)

// Setup connections before registrar
const [localPeer, remotePeer] = await peerUtils.createPeerInfo({ number: 2 })

const conn1 = await createMockConnection({ localPeer: localPeer.id, remotePeer: remotePeer.id })
const conn2 = await createMockConnection({ localPeer: localPeer.id, remotePeer: remotePeer.id })
const peerInfo = await PeerInfo.create(remotePeer.id)
const id = peerInfo.id.toString()

// Add connection to registrar
peerStore.put(peerInfo)
registrar.onConnect(peerInfo, conn1)
registrar.onConnect(peerInfo, conn2)

expect(registrar.connections.get(id).length).to.eql(2)

conn2._stat.status = 'closed'
registrar.onDisconnect(peerInfo, conn2)

const peerConnections = registrar.connections.get(id)
expect(peerConnections.length).to.eql(1)
expect(peerConnections[0]._stat.status).to.eql('open')
})
})
})
3 changes: 2 additions & 1 deletion test/registrar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports.createMockConnection = async (properties = {}) => {
},
direction: 'outbound',
encryption: '/secio/1.0.0',
multiplexer: '/mplex/6.7.0'
multiplexer: '/mplex/6.7.0',
status: 'open'
},
newStream: (protocols) => {
const id = streamId++
Expand Down