Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optimistic mirror of kbuckets with read lock only #229

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/discv5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,33 @@ impl<P: ProtocolIdentity> Discv5<P> {
.collect()
}

/// Takes a closure parameterized by type `Arc<RwLock<KBucketsTable<NodeId, Enr>>>` as
/// parameter. Caution: caller is responsible of dropping a lock taken on the kbuckets. For
/// example, a read lock can be taken on the kbuckets to optimistically view the current keys
/// in the kbuckets (optimistic since it doesn't apply pending entries, which requires a write
/// lock).
/// ```
/// use std::str::FromStr;
/// use discv5::{ConfigBuilder, Discv5, ListenConfig, Enr, enr::CombinedKey};
///
/// let sk = CombinedKey::generate_secp256k1();
/// let enr = Enr::builder().build(&sk).unwrap();
/// let config = ConfigBuilder::new(ListenConfig::default()).build();
/// let discv5: Discv5 = Discv5::new(enr, sk, config).unwrap();
///
/// let entries = discv5.with_kbuckets(|kbuckets| kbuckets
/// .read()
/// .iter_ref()
/// .map(|entry| *entry.node.key.preimage())
/// .collect::<Vec<_>>());
/// ```
pub fn with_kbuckets<F, T>(&self, f: F) -> T
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced the methods for this one which covers all cases

where
F: FnOnce(&Arc<RwLock<KBucketsTable<NodeId, Enr>>>) -> T,
{
f(&self.kbuckets)
}

/// 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
Loading