Skip to content

Commit

Permalink
Allow optimistic mirror of kbuckets with read lock only
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Jan 22, 2024
1 parent f0220e2 commit a29538d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/discv5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ impl<P: ProtocolIdentity> Discv5<P> {
.collect()
}

/// Takes only a read lock on the kbuckets. This means pending entries aren't added. Otherwise
/// same as [`Self::table_entries_id`]. Returns an iterator over all ENR node IDs of nodes
/// currently contained in the routing table.
pub fn table_entries_id_rlock(&self) -> Vec<NodeId> {
self.kbuckets
.read()
.iter_ref()
.map(|entry| *entry.node.key.preimage())
.collect()
}

/// Returns an iterator over all the ENR's of nodes currently contained in the routing table.
pub fn table_entries_enr(&self) -> Vec<Enr> {
self.kbuckets
Expand All @@ -477,6 +488,23 @@ impl<P: ProtocolIdentity> Discv5<P> {
.collect()
}

/// Takes only a read lock on the kbuckets. This means pending entries aren't added. Otherwise
/// same as [`Self::table_entries`]. Returns an iterator over all ENR node IDs of nodes
/// currently contained in the routing table.
pub fn table_entries_rlock(&self) -> Vec<(NodeId, Enr, NodeStatus)> {
self.kbuckets
.read()
.iter_ref()
.map(|entry| {
(
*entry.node.key.preimage(),
entry.node.value.clone(),
entry.status,
)
})
.collect()
}

/// Requests the ENR of a node corresponding to multiaddr or multi-addr string.
///
/// Only `ed25519` and `secp256k1` key types are currently supported.
Expand Down
2 changes: 1 addition & 1 deletion src/handler/request_call.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::node_info::{NodeAddress, NodeContact};
pub use crate::node_info::NodeContact;
use crate::{
packet::Packet,
rpc::{Request, RequestBody},
Expand Down
2 changes: 1 addition & 1 deletion src/kbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ where

/// Returns an iterator over all the entries in the routing table.
/// Does not add pending node to kbucket to get an iterator which
/// takes a reference instead of a mutable reference.
/// takes a mutable reference instead of a reference.
pub fn iter_ref(&self) -> impl Iterator<Item = EntryRefView<'_, TNodeId, TVal>> {
self.buckets.iter().flat_map(move |table| {
table.iter().map(move |n| EntryRefView {
Expand Down
4 changes: 1 addition & 3 deletions src/kbucket/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
//! representing the nodes participating in the Kademlia DHT.

pub use super::{
bucket::{
AppliedPending, ConnectionState, InsertResult, Node, NodeStatus, MAX_NODES_PER_BUCKET,
},
bucket::{AppliedPending, ConnectionState, InsertResult, Node, NodeStatus},
key::*,
ConnectionDirection,
};
Expand Down

0 comments on commit a29538d

Please sign in to comment.