|
@@ -173,6 +173,9 @@ pub async fn dht_refinery_task<H: DhtHandler>(handler: Arc<H>) -> Result<()> {
|
|
|
/// If the bucket is already full, we ping the least recently seen node in the
|
|
/// If the bucket is already full, we ping the least recently seen node in the
|
|
|
/// bucket: if successful it becomes the most recently seen node, if the ping
|
|
/// bucket: if successful it becomes the most recently seen node, if the ping
|
|
|
/// fails we remove it and add the new node.
|
|
/// fails we remove it and add the new node.
|
|
|
|
|
+/// [`Dht::update_node()`] increments a channel's usage count (in the direct
|
|
|
|
|
+/// session) and triggers this task. This task decrements the usage count
|
|
|
|
|
+/// using [`Dht::cleanup_channel()`].
|
|
|
pub async fn add_node_task<H: DhtHandler>(handler: Arc<H>) -> Result<()> {
|
|
pub async fn add_node_task<H: DhtHandler>(handler: Arc<H>) -> Result<()> {
|
|
|
let dht = handler.dht();
|
|
let dht = handler.dht();
|
|
|
loop {
|
|
loop {
|
|
@@ -187,17 +190,20 @@ pub async fn add_node_task<H: DhtHandler>(handler: Arc<H>) -> Result<()> {
|
|
|
|
|
|
|
|
// Do not add ourselves to the buckets
|
|
// Do not add ourselves to the buckets
|
|
|
if node.id() == self_node.id() {
|
|
if node.id() == self_node.id() {
|
|
|
|
|
+ dht.cleanup_channel(channel).await;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Don't add this node if it has any external address that is the same as one of ours
|
|
// Don't add this node if it has any external address that is the same as one of ours
|
|
|
let node_addresses = node.addresses();
|
|
let node_addresses = node.addresses();
|
|
|
if self_node.addresses().iter().any(|addr| node_addresses.contains(addr)) {
|
|
if self_node.addresses().iter().any(|addr| node_addresses.contains(addr)) {
|
|
|
|
|
+ dht.cleanup_channel(channel).await;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Do not add a node to the buckets if it does not have an address
|
|
// Do not add a node to the buckets if it does not have an address
|
|
|
if node.addresses().is_empty() {
|
|
if node.addresses().is_empty() {
|
|
|
|
|
+ dht.cleanup_channel(channel).await;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -205,19 +211,21 @@ pub async fn add_node_task<H: DhtHandler>(handler: Arc<H>) -> Result<()> {
|
|
|
if let Some(node_index) = bucket.nodes.iter().position(|n| n.id() == node.id()) {
|
|
if let Some(node_index) = bucket.nodes.iter().position(|n| n.id() == node.id()) {
|
|
|
bucket.nodes.remove(node_index);
|
|
bucket.nodes.remove(node_index);
|
|
|
bucket.nodes.push(node);
|
|
bucket.nodes.push(node);
|
|
|
|
|
+ dht.cleanup_channel(channel).await;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Bucket is full
|
|
// Bucket is full
|
|
|
- if bucket.nodes.len() >= handler.dht().settings.k {
|
|
|
|
|
|
|
+ if bucket.nodes.len() >= dht.settings.k {
|
|
|
// Ping the least recently seen node
|
|
// Ping the least recently seen node
|
|
|
- if let Ok((channel, node)) = handler.dht().get_channel(&bucket.nodes[0]).await {
|
|
|
|
|
|
|
+ if let Ok((channel2, node)) = dht.get_channel(&bucket.nodes[0]).await {
|
|
|
// Ping was successful, move the least recently seen node to the tail
|
|
// Ping was successful, move the least recently seen node to the tail
|
|
|
let n = bucket.nodes.remove(0);
|
|
let n = bucket.nodes.remove(0);
|
|
|
bucket.nodes.push(n);
|
|
bucket.nodes.push(n);
|
|
|
drop(buckets);
|
|
drop(buckets);
|
|
|
- dht.on_new_node(&node.clone(), channel.clone()).await;
|
|
|
|
|
- handler.dht().cleanup_channel(channel).await;
|
|
|
|
|
|
|
+ dht.on_new_node(&node.clone(), channel2.clone()).await;
|
|
|
|
|
+ dht.cleanup_channel(channel2).await;
|
|
|
|
|
+ dht.cleanup_channel(channel).await;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -226,6 +234,7 @@ pub async fn add_node_task<H: DhtHandler>(handler: Arc<H>) -> Result<()> {
|
|
|
bucket.nodes.push(node.clone());
|
|
bucket.nodes.push(node.clone());
|
|
|
drop(buckets);
|
|
drop(buckets);
|
|
|
dht.on_new_node(&node.clone(), channel.clone()).await;
|
|
dht.on_new_node(&node.clone(), channel.clone()).await;
|
|
|
|
|
+ dht.cleanup_channel(channel).await;
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -233,6 +242,7 @@ pub async fn add_node_task<H: DhtHandler>(handler: Arc<H>) -> Result<()> {
|
|
|
bucket.nodes.push(node.clone());
|
|
bucket.nodes.push(node.clone());
|
|
|
drop(buckets);
|
|
drop(buckets);
|
|
|
dht.on_new_node(&node.clone(), channel.clone()).await;
|
|
dht.on_new_node(&node.clone(), channel.clone()).await;
|
|
|
|
|
+ dht.cleanup_channel(channel).await;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|