bucket: apply_pending WIP

This commit is contained in:
folex
2020-03-20 18:46:47 +03:00
parent 0d66695d3b
commit c096db5e59

View File

@ -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); // A disconnected pending node goes at the end of the list
return Some(AppliedPending { inserted, evicted }) // for the disconnected peers.
} let evicted = self.pop_node();
// A disconnected pending node goes at the end of the list self.insert_disconnected_node(pending.node.clone());
// for the disconnected peers.
else if let Some(p) = self.first_connected_pos { Some(AppliedPending { inserted: pending.node, evicted: Some(evicted) })
let insert_pos = p.checked_sub(1).expect("by (*)"); },
let evicted = Some(self.nodes.remove(0));
self.nodes.insert(insert_pos, pending.node);
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.