@@ -384,11 +384,11 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
384
384
return nil , ErrSwarmClosed
385
385
}
386
386
387
- oldState := s .connectednessUnlocked (p )
387
+ oldState := s .connectednessUnlocked (p , true )
388
388
389
389
c .streams .m = make (map [* Stream ]struct {})
390
390
s .conns .m [p ] = append (s .conns .m [p ], c )
391
- newState := s .connectednessUnlocked (p )
391
+ newState := s .connectednessUnlocked (p , true )
392
392
393
393
// Add two swarm refs:
394
394
// * One will be decremented after the close notifications fire in Conn.doClose
@@ -653,12 +653,20 @@ func (s *Swarm) Connectedness(p peer.ID) network.Connectedness {
653
653
s .conns .RLock ()
654
654
defer s .conns .RUnlock ()
655
655
656
- return s .connectednessUnlocked (p )
656
+ return s .connectednessUnlocked (p , false )
657
657
}
658
658
659
- func (s * Swarm ) connectednessUnlocked (p peer.ID ) network.Connectedness {
659
+ // connectednessUnlocked returns the connectedness of a peer. For sending peer connectedness
660
+ // changed notifications consider closed connections. When remote closes a connection
661
+ // the underlying transport connection is closed first, so tracking change to the connectedness
662
+ // state requires considering this recently closed connections impact on Connectedness.
663
+ func (s * Swarm ) connectednessUnlocked (p peer.ID , considerClosed bool ) network.Connectedness {
660
664
var haveTransient bool
661
665
for _ , c := range s .conns .m [p ] {
666
+ if ! considerClosed && c .IsClosed () {
667
+ // These will be garbage collected soon
668
+ continue
669
+ }
662
670
if c .Stat ().Transient {
663
671
haveTransient = true
664
672
} else {
@@ -768,7 +776,7 @@ func (s *Swarm) removeConn(c *Conn) {
768
776
769
777
cs := s .conns .m [p ]
770
778
771
- oldState := s .connectednessUnlocked (p )
779
+ oldState := s .connectednessUnlocked (p , true )
772
780
773
781
if len (cs ) == 1 {
774
782
delete (s .conns .m , p )
@@ -786,7 +794,7 @@ func (s *Swarm) removeConn(c *Conn) {
786
794
}
787
795
}
788
796
789
- newState := s .connectednessUnlocked (p )
797
+ newState := s .connectednessUnlocked (p , true )
790
798
791
799
s .conns .Unlock ()
792
800
0 commit comments