Commit 86b1e564 authored by Petar Radovic's avatar Petar Radovic Committed by GitHub

Peer registry fix - multiple connections (#570)

* peer registry multiple connections case
parent c1af7d40
......@@ -59,15 +59,17 @@ func (r *peerRegistry) Disconnected(_ network.Network, c network.Conn) {
return
}
overlay := r.overlays[peerID]
delete(r.overlays, peerID)
delete(r.underlays, overlay.ByteString())
// if there are multiple libp2p connections, consider the node disconnected only when the last connection is disconnected
delete(r.connections[peerID], c)
if len(r.connections[peerID]) == 0 {
delete(r.connections, peerID)
if len(r.connections[peerID]) > 0 {
r.mu.Unlock()
return
}
delete(r.connections, peerID)
overlay := r.overlays[peerID]
delete(r.overlays, peerID)
delete(r.underlays, overlay.ByteString())
for _, cancel := range r.streams[peerID] {
cancel()
}
......@@ -131,14 +133,17 @@ func (r *peerRegistry) addIfNotExists(c network.Conn, overlay swarm.Address) (ex
r.mu.Lock()
defer r.mu.Unlock()
if _, exists := r.underlays[overlay.ByteString()]; exists {
return true
}
if _, ok := r.connections[peerID]; !ok {
r.connections[peerID] = make(map[network.Conn]struct{})
}
// the connection is added even if the peer already exists in peer registry
// this is solving a case of multiple underlying libp2p connections for the same peer
r.connections[peerID][c] = struct{}{}
if _, exists := r.underlays[overlay.ByteString()]; exists {
return true
}
r.streams[peerID] = make(map[network.Stream]context.CancelFunc)
r.underlays[overlay.ByteString()] = peerID
r.overlays[peerID] = overlay
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment