MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / take_for_message

Method take_for_message

nodedb-cluster/src/swim/dissemination/queue.rs:70–101  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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`].

Callers 10

executeMethod · 0.80
handle_pingMethod · 0.80
handle_ping_reqMethod · 0.80
take_caps_at_maxFunction · 0.80
least_disseminated_winsFunction · 0.80

Calls 12

lockMethod · 0.80
collectMethod · 0.80
expectMethod · 0.45
cmpMethod · 0.45
as_strMethod · 0.45
truncateMethod · 0.45
lenMethod · 0.45
get_mutMethod · 0.45
record_sentMethod · 0.45
pushMethod · 0.45
cloneMethod · 0.45
removeMethod · 0.45