Commit 395fb26a authored by Steven Allen's avatar Steven Allen

comment on why we lock when handling (dis)connect notifs

parent 64b46c1d
......@@ -26,6 +26,9 @@ func (nn *netNotifiee) Connected(n inet.Network, v inet.Conn) {
p := v.RemotePeer()
protos, err := dht.peerstore.SupportsProtocols(p, dhtProtocols...)
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()
defer dht.plk.Unlock()
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)
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()
defer dht.plk.Unlock()
// Make sure we're still connected under the lock (race with disconnect)
if dht.host.Network().Connectedness(p) == inet.Connected {
dht.Update(dht.Context(), p)
}
......@@ -80,6 +85,8 @@ func (nn *netNotifiee) Disconnected(n inet.Network, v inet.Conn) {
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()
defer dht.plk.Unlock()
if dht.host.Network().Connectedness(p) == inet.Connected {
......
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