Skip to content

Commit

Permalink
updated connectednessUnlocked
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumemichel committed Mar 22, 2024
1 parent fde15f9 commit 884c093
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,6 @@ func (s *Swarm) Connectedness(p peer.ID) network.Connectedness {
func (s *Swarm) connectednessUnlocked(p peer.ID) network.Connectedness {
var haveTransient bool
for _, c := range s.conns.m[p] {
if c.conn.IsClosed() {
// We *will* garbage collect this soon anyways.
continue
}
if c.Stat().Transient {
haveTransient = true
} else {
Expand Down Expand Up @@ -776,28 +772,20 @@ func (s *Swarm) removeConn(c *Conn) {

if len(cs) == 1 {
delete(s.conns.m, p)
s.conns.Unlock()

// Emit event after releasing `s.conns` lock so that a consumer can still
// use swarm methods that need the `s.conns` lock.
s.emitter.Emit(event.EvtPeerConnectednessChanged{
Peer: p,
Connectedness: network.NotConnected,
})
return
}

for i, ci := range cs {
if ci == c {
// NOTE: We're intentionally preserving order.
// This way, connections to a peer are always
// sorted oldest to newest.
copy(cs[i:], cs[i+1:])
cs[len(cs)-1] = nil
s.conns.m[p] = cs[:len(cs)-1]
break
} else {
for i, ci := range cs {
if ci == c {
// NOTE: We're intentionally preserving order.
// This way, connections to a peer are always
// sorted oldest to newest.
copy(cs[i:], cs[i+1:])
cs[len(cs)-1] = nil
s.conns.m[p] = cs[:len(cs)-1]
break
}
}
}

newState := s.connectednessUnlocked(p)

s.conns.Unlock()
Expand Down

0 comments on commit 884c093

Please sign in to comment.