Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix a race cond in dial
  • Loading branch information
mkg20001 committed May 19, 2018
commit f2c3a005a1ea20ad0c16be25c3b05c4d8963e137
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RendezvousDiscovery {
this.swarm = swarm
this.rpc = []
this.rpcById = {}
this.rpcRace = {}
this.swarm.on('peer:connect', peer => {
this._dial(peer)
})
Expand All @@ -29,10 +30,11 @@ class RendezvousDiscovery {
cb = once(cb)
if (!this.state) return cb()
this._cleanPeers()
if (this.rpcById[pi.id.toB58String()]) {
if (this.rpcById[pi.id.toB58String()] || this.rpcRace[pi.id.toB58String()]) {
log('skip reconnecting %s', pi.id.toB58String())
return cb()
}
this.rpcRace[pi.id.toB58String()] = true
this.swarm.dialProtocol(pi, '/rendezvous/1.0.0', (err, conn) => {
if (err) return cb(err)
const rpc = new RPC()
Expand All @@ -46,6 +48,7 @@ class RendezvousDiscovery {
rpc.registrations = {}

log('add new peer %s', rpc.id)
delete this.rpcRace[pi.id.toB58String()]
this._syncAll(cb)
})
})
Expand Down Expand Up @@ -120,6 +123,7 @@ class RendezvousDiscovery {
this.state = null
this.rpc = []
this.rpcById = {}
this.rpcRace = {}
cb()
}
}
Expand Down