Return up to `max` least-disseminated updates for a single outgoing message. Increments each returned entry's `sent_count` and drops entries whose new count has reached `lambda_log_n`. `lambda_log_n` is `ceil(lambda * log2(cluster_size + 1))` — computed by the caller because it depends on the current membership size and the [`super::super::config::SwimConfig`] `fanout_lambda` knob.
(&self, max: usize, lambda_log_n: u32)
| 68 | /// membership size and the [`super::super::config::SwimConfig`] |
| 69 | /// `fanout_lambda` knob. |
| 70 | pub fn take_for_message(&self, max: usize, lambda_log_n: u32) -> Vec<MemberUpdate> { |
| 71 | if max == 0 { |
| 72 | return Vec::new(); |
| 73 | } |
| 74 | let mut guard = self.inner.lock().expect("dissemination lock poisoned"); |
| 75 | |
| 76 | // Sort a snapshot of keys by (sent_count, node_id) ascending; |
| 77 | // we can't use BinaryHeap directly without cloning because the |
| 78 | // values need to be mutated in place after the decision. |
| 79 | let mut keys: Vec<NodeId> = guard.keys().cloned().collect(); |
| 80 | keys.sort_by(|a, b| { |
| 81 | let pa = &guard[a]; |
| 82 | let pb = &guard[b]; |
| 83 | pa.sent_count |
| 84 | .cmp(&pb.sent_count) |
| 85 | .then_with(|| a.as_str().cmp(b.as_str())) |
| 86 | .then_with(|| pa.update.incarnation.cmp(&pb.update.incarnation)) |
| 87 | }); |
| 88 | keys.truncate(max); |
| 89 | |
| 90 | let mut out = Vec::with_capacity(keys.len()); |
| 91 | for k in keys { |
| 92 | if let Some(pending) = guard.get_mut(&k) { |
| 93 | pending.record_sent(); |
| 94 | out.push(pending.update.clone()); |
| 95 | if pending.sent_count >= lambda_log_n { |
| 96 | guard.remove(&k); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | out |
| 101 | } |
| 102 | |
| 103 | /// Compute `ceil(lambda * log2(cluster_size + 1))`. Exposed so the |
| 104 | /// runner can pass the result straight into [`take_for_message`]. |