From b18c75f20f937229c9b12bf3f07901090bd35317 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 6 Nov 2026 16:34:47 +0100 Subject: topology trait --- src/swarm/topology/graph.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/swarm/topology/graph.rs') diff --git a/src/swarm/topology/graph.rs b/src/swarm/topology/graph.rs index 5401506..92240e9 100644 --- a/src/swarm/topology/graph.rs +++ b/src/swarm/topology/graph.rs @@ -35,6 +35,7 @@ pub(crate) fn schemes(addresses: &[String]) -> BTreeSet { /// `local_transports`: on each, its neighbours among the members on it /// (`neighbours`), all together. pub(crate) fn neighbours_across( + topology: &dyn Topology, members: &BTreeMap>, local: &str, local_transports: &BTreeSet, @@ -49,7 +50,7 @@ pub(crate) fn neighbours_across( .filter(|(member, transports)| *member != local && transports.contains(transport)) .map(|(member, _)| member.clone()) .collect(); - all.extend(neighbours(&on_it, local, k, unreachable, stable)); + all.extend(neighbours(topology, &on_it, local, k, unreachable, stable)); } all } @@ -57,6 +58,7 @@ pub(crate) fn neighbours_across( /// The neighbours of `local` among `members` (which may or may not contain `local`), /// skipping members for which `unreachable` holds. pub(crate) fn neighbours( + topology: &dyn Topology, members: &[String], local: &str, k: usize, @@ -65,11 +67,32 @@ pub(crate) fn neighbours( ) -> BTreeSet { let mut all: Vec = members.to_vec(); all.push(local.to_owned()); - graph(&all, k, &|m: &str| m != local && unreachable(m), stable) + topology + .graph(&all, k, &|m: &str| m != local && unreachable(m), stable) .remove(local) .unwrap_or_default() } +/// The graph described above (`graph`): a ring by key hash, plus each member's +/// lowest-scoring pairs, stable members preferred. +pub(crate) struct RingAndPairs; + +impl Topology for RingAndPairs { + fn name(&self) -> &'static str { + "ring-and-pairs" + } + + fn graph( + &self, + members: &[String], + k: usize, + unreachable: &dyn Fn(&str) -> bool, + stable: &dyn Fn(&str) -> bool, + ) -> BTreeMap> { + graph(members, k, unreachable, stable) + } +} + /// Every reachable member's neighbours. Hashes each member once and scores pairs by /// mixing the two hashes (`pair_score`), so the N² scores are a few nanoseconds each: /// tens of milliseconds for thousands of members. `stable` members (`swarm::stability`) -- cgit v1.3.1