mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-20 05:16:35 +00:00
bucket: apply_pending WIP
This commit is contained in:
@ -216,37 +216,28 @@ where
|
|||||||
if let Some(pending) = self.pending.take() {
|
if let Some(pending) = self.pending.take() {
|
||||||
if pending.replace <= Instant::now() {
|
if pending.replace <= Instant::now() {
|
||||||
if self.nodes.is_full() {
|
if self.nodes.is_full() {
|
||||||
if self.status(Position(0)) == NodeStatus::Connected {
|
if self.all_nodes_connected() {
|
||||||
// The bucket is full with connected nodes. Drop the pending node.
|
// The bucket is full with connected nodes. Drop the pending node.
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
debug_assert!(self.first_connected_pos.map_or(true, |p| p > 0)); // (*)
|
debug_assert!(self.first_connected_pos.map_or(true, |p| p > 0)); // (*)
|
||||||
// The pending node will be inserted.
|
return match pending.status {
|
||||||
let inserted = pending.node.clone();
|
NodeStatus::Connected => {
|
||||||
// A connected pending node goes at the end of the list for
|
// A connected pending node goes at the end of the list for
|
||||||
// the connected peers, removing the least-recently connected.
|
// the connected peers, removing the least-recently connected.
|
||||||
if pending.status == NodeStatus::Connected {
|
let evicted = self.pop_node();
|
||||||
let evicted = Some(self.nodes.remove(0));
|
self.append_connected_node(pending.node.clone());
|
||||||
self.first_connected_pos = self.first_connected_pos
|
|
||||||
.map_or_else(
|
Some(AppliedPending { inserted: pending.node, evicted: Some(evicted) })
|
||||||
| | Some(self.nodes.len()),
|
},
|
||||||
|p| p.checked_sub(1));
|
NodeStatus::Disconnected => {
|
||||||
self.nodes.push(pending.node);
|
|
||||||
return Some(AppliedPending { inserted, evicted })
|
|
||||||
}
|
|
||||||
// A disconnected pending node goes at the end of the list
|
// A disconnected pending node goes at the end of the list
|
||||||
// for the disconnected peers.
|
// for the disconnected peers.
|
||||||
else if let Some(p) = self.first_connected_pos {
|
let evicted = self.pop_node();
|
||||||
let insert_pos = p.checked_sub(1).expect("by (*)");
|
self.insert_disconnected_node(pending.node.clone());
|
||||||
let evicted = Some(self.nodes.remove(0));
|
|
||||||
self.nodes.insert(insert_pos, pending.node);
|
Some(AppliedPending { inserted: pending.node, evicted: Some(evicted) })
|
||||||
return Some(AppliedPending { inserted, evicted })
|
},
|
||||||
} else {
|
|
||||||
// All nodes are disconnected. Insert the new node as the most
|
|
||||||
// recently disconnected, removing the least-recently disconnected.
|
|
||||||
let evicted = Some(self.nodes.remove(0));
|
|
||||||
self.nodes.push(pending.node);
|
|
||||||
return Some(AppliedPending { inserted, evicted })
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// There is room in the bucket, so just insert the pending node.
|
// There is room in the bucket, so just insert the pending node.
|
||||||
|
Reference in New Issue
Block a user