mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-04-25 14:52:14 +00:00
comment on why we lock when handling (dis)connect notifs
This commit is contained in:
parent
64b46c1d79
commit
395fb26a0e
9
notif.go
9
notif.go
@ -26,6 +26,9 @@ func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
|
|||||||
p := v.RemotePeer()
|
p := v.RemotePeer()
|
||||||
protos, err := dht.peerstore.SupportsProtocols(p, dhtProtocols...)
|
protos, err := dht.peerstore.SupportsProtocols(p, dhtProtocols...)
|
||||||
if err == nil && len(protos) != 0 {
|
if err == nil && len(protos) != 0 {
|
||||||
|
// We lock here for consistency with the lock in testConnection.
|
||||||
|
// This probably isn't necessary because (dis)connect
|
||||||
|
// notifications are serialized but it's nice to be consistent.
|
||||||
dht.plk.Lock()
|
dht.plk.Lock()
|
||||||
defer dht.plk.Unlock()
|
defer dht.plk.Unlock()
|
||||||
if dht.host.Network().Connectedness(p) == inet.Connected {
|
if dht.host.Network().Connectedness(p) == inet.Connected {
|
||||||
@ -62,9 +65,11 @@ func (nn *netNotifiee) testConnection(v inet.Conn) {
|
|||||||
// Remember this choice (makes subsequent negotiations faster)
|
// Remember this choice (makes subsequent negotiations faster)
|
||||||
dht.peerstore.AddProtocols(p, selected)
|
dht.peerstore.AddProtocols(p, selected)
|
||||||
|
|
||||||
|
// We lock here as we race with disconnect. If we didn't lock, we could
|
||||||
|
// finish processing a connect after handling the associated disconnect
|
||||||
|
// event and add the peer to the routing table after removing it.
|
||||||
dht.plk.Lock()
|
dht.plk.Lock()
|
||||||
defer dht.plk.Unlock()
|
defer dht.plk.Unlock()
|
||||||
// Make sure we're still connected under the lock (race with disconnect)
|
|
||||||
if dht.host.Network().Connectedness(p) == inet.Connected {
|
if dht.host.Network().Connectedness(p) == inet.Connected {
|
||||||
dht.Update(dht.Context(), p)
|
dht.Update(dht.Context(), p)
|
||||||
}
|
}
|
||||||
@ -80,6 +85,8 @@ func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
|
|||||||
|
|
||||||
p := v.RemotePeer()
|
p := v.RemotePeer()
|
||||||
|
|
||||||
|
// Lock and check to see if we're still connected. We lock to make sure
|
||||||
|
// we don't concurrently process a connect event.
|
||||||
dht.plk.Lock()
|
dht.plk.Lock()
|
||||||
defer dht.plk.Unlock()
|
defer dht.plk.Unlock()
|
||||||
if dht.host.Network().Connectedness(p) == inet.Connected {
|
if dht.host.Network().Connectedness(p) == inet.Connected {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user