swarm: emit connectedness events when holding the mutex
This commit is contained in:
parent
9f32bc2269
commit
7889a65ece
|
@ -329,7 +329,12 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.streams.m = make(map[*Stream]struct{})
|
c.streams.m = make(map[*Stream]struct{})
|
||||||
isFirstConn := len(s.conns.m[p]) == 0
|
if len(s.conns.m[p]) == 0 { // first connection
|
||||||
|
s.emitter.Emit(event.EvtPeerConnectednessChanged{
|
||||||
|
Peer: p,
|
||||||
|
Connectedness: network.Connected,
|
||||||
|
})
|
||||||
|
}
|
||||||
s.conns.m[p] = append(s.conns.m[p], c)
|
s.conns.m[p] = append(s.conns.m[p], c)
|
||||||
|
|
||||||
// Add two swarm refs:
|
// Add two swarm refs:
|
||||||
|
@ -347,13 +352,6 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
|
||||||
})
|
})
|
||||||
c.notifyLk.Unlock()
|
c.notifyLk.Unlock()
|
||||||
|
|
||||||
if isFirstConn {
|
|
||||||
s.emitter.Emit(event.EvtPeerConnectednessChanged{
|
|
||||||
Peer: p,
|
|
||||||
Connectedness: network.Connected,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
c.start()
|
c.start()
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
@ -627,14 +625,18 @@ func (s *Swarm) StopNotify(f network.Notifiee) {
|
||||||
func (s *Swarm) removeConn(c *Conn) {
|
func (s *Swarm) removeConn(c *Conn) {
|
||||||
p := c.RemotePeer()
|
p := c.RemotePeer()
|
||||||
|
|
||||||
var disconnected bool
|
|
||||||
s.conns.Lock()
|
s.conns.Lock()
|
||||||
|
defer s.conns.Unlock()
|
||||||
|
|
||||||
cs := s.conns.m[p]
|
cs := s.conns.m[p]
|
||||||
for i, ci := range cs {
|
for i, ci := range cs {
|
||||||
if ci == c {
|
if ci == c {
|
||||||
if len(cs) == 1 {
|
if len(cs) == 1 {
|
||||||
delete(s.conns.m, p)
|
delete(s.conns.m, p)
|
||||||
disconnected = true
|
s.emitter.Emit(event.EvtPeerConnectednessChanged{
|
||||||
|
Peer: p,
|
||||||
|
Connectedness: network.NotConnected,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
// NOTE: We're intentionally preserving order.
|
// NOTE: We're intentionally preserving order.
|
||||||
// This way, connections to a peer are always
|
// This way, connections to a peer are always
|
||||||
|
@ -646,14 +648,6 @@ func (s *Swarm) removeConn(c *Conn) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.conns.Unlock()
|
|
||||||
|
|
||||||
if disconnected {
|
|
||||||
s.emitter.Emit(event.EvtPeerConnectednessChanged{
|
|
||||||
Peer: p,
|
|
||||||
Connectedness: network.NotConnected,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a string representation of Network.
|
// String returns a string representation of Network.
|
||||||
|
|
Loading…
Reference in New Issue