Skip to content

Commit

Permalink
networking: Fix get_closest_peers in batches.
Browse files Browse the repository at this point in the history
  • Loading branch information
shamil-gadelshin committed Dec 20, 2023
1 parent 9d6a432 commit a3a19e1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions crates/subspace-networking/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,21 @@ impl Node {
&self,
key: Multihash,
) -> Result<impl Stream<Item = PeerId>, GetClosestPeersError> {
self.get_closest_peers_internal(key).await
self.get_closest_peers_internal(key, true).await
}

/// Get closest peers by multihash key using Kademlia DHT.
async fn get_closest_peers_internal(
&self,
key: Multihash,
acquire_permit: bool,
) -> Result<impl Stream<Item = PeerId>, GetClosestPeersError> {
let permit = self.shared.rate_limiter.acquire_kademlia_permit().await;
let permit = if acquire_permit {
Some(self.shared.rate_limiter.acquire_kademlia_permit().await)
} else {
None
};

trace!(?key, "Starting 'GetClosestPeers' request.");

let (result_sender, result_receiver) = mpsc::unbounded();
Expand Down Expand Up @@ -602,7 +608,7 @@ impl NodeRequestsBatchHandle {
&mut self,
key: Multihash,
) -> Result<impl Stream<Item = PeerId>, GetClosestPeersError> {
self.node.get_closest_peers_internal(key).await
self.node.get_closest_peers_internal(key, false).await
}
/// Sends the generic request to the peer and awaits the result.
pub async fn send_generic_request<Request>(
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum QueryResultSender {
ClosestPeers {
sender: mpsc::UnboundedSender<PeerId>,
// Just holding onto permit while data structure is not dropped
_permit: RateLimiterPermit,
_permit: Option<RateLimiterPermit>,
},
Providers {
sender: mpsc::UnboundedSender<PeerId>,
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-networking/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub(crate) enum Command {
GetClosestPeers {
key: Multihash,
result_sender: mpsc::UnboundedSender<PeerId>,
permit: RateLimiterPermit,
permit: Option<RateLimiterPermit>,
},
GenericRequest {
peer_id: PeerId,
Expand Down

0 comments on commit a3a19e1

Please sign in to comment.