From 44969b25504a4c91f45cac902f264579e8b90ca8 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Fri, 17 Nov 2023 15:26:40 +0300 Subject: p2p: avoid doing too much locking and unlocking for the routing table mutex --- p2p/src/discovery/lookup.rs | 10 +++++++--- p2p/src/discovery/refresh.rs | 4 +--- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'p2p/src/discovery') diff --git a/p2p/src/discovery/lookup.rs b/p2p/src/discovery/lookup.rs index 52aa339..0138068 100644 --- a/p2p/src/discovery/lookup.rs +++ b/p2p/src/discovery/lookup.rs @@ -140,11 +140,12 @@ impl LookupService { } } + let mut table = self.table.lock().await; for peer in peer_buffer.iter() { - let mut table = self.table.lock().await; let result = table.add_entry(peer.clone().into()); trace!("Add entry {:?}", result); } + drop(table); self.monitor .notify(&DiscoveryEvent::LookupSucceeded(endpoint.clone(), peer_buffer.len()).into()) @@ -164,10 +165,12 @@ impl LookupService { for _ in 0..2 { let peer_id = PeerID::random(); let peers = self.connect(&peer_id, endpoint.clone()).await?; + + let table = self.table.lock().await; for peer in peers { if random_peers.contains(&peer) || peer.peer_id == self.id - || self.table.lock().await.contains_key(&peer.peer_id.0) + || table.contains_key(&peer.peer_id.0) { continue; } @@ -343,8 +346,9 @@ impl LookupService { trace!("Send Peers msg"); let table = self.table.lock().await; let entries = table.closest_entries(&peer_id.0, MAX_PEERS_IN_PEERSMSG); - let peers: Vec = entries.into_iter().map(|e| e.into()).collect(); drop(table); + + let peers: Vec = entries.into_iter().map(|e| e.into()).collect(); io_codec.write(NetMsgCmd::Peers, &PeersMsg(peers)).await?; Ok(()) } diff --git a/p2p/src/discovery/refresh.rs b/p2p/src/discovery/refresh.rs index a708261..1ced266 100644 --- a/p2p/src/discovery/refresh.rs +++ b/p2p/src/discovery/refresh.rs @@ -130,14 +130,12 @@ impl RefreshService { .notify(&DiscoveryEvent::RefreshStarted.into()) .await; - let table = self.table.lock().await; let mut entries: Vec = vec![]; - for bucket in table.iter() { + for bucket in self.table.lock().await.iter() { for entry in bucket.random_iter(8) { entries.push(entry.clone()) } } - drop(table); self.clone().do_refresh(&entries).await; } -- cgit v1.2.3