diff options
Diffstat (limited to 'src/swarm/topology.rs')
| -rw-r--r-- | src/swarm/topology.rs | 23 |
1 files changed, 6 insertions, 17 deletions
diff --git a/src/swarm/topology.rs b/src/swarm/topology.rs index 0dae7e9..73c2f2c 100644 --- a/src/swarm/topology.rs +++ b/src/swarm/topology.rs @@ -23,6 +23,7 @@ use sha2::{Digest, Sha256}; use tokio::time::Instant; use tracing::info; +use crate::locks::RwLockExt; use crate::state::AppState; use crate::swarm::members; @@ -42,8 +43,7 @@ pub(crate) struct NeighbourView { pub(crate) fn is_full_mesh(state: &AppState) -> bool { state .neighbours - .read() - .unwrap_or_else(|poisoned| poisoned.into_inner()) + .read_or_recover() .as_ref() .is_none_or(|view| view.full_mesh) } @@ -65,8 +65,7 @@ pub(crate) fn summary(state: &AppState) -> Option<Summary> { let local = state.local_id(); state .neighbours - .read() - .unwrap_or_else(|poisoned| poisoned.into_inner()) + .read_or_recover() .as_ref() .map(|view| Summary { members: view.members.len(), @@ -91,11 +90,7 @@ pub(crate) fn summary(state: &AppState) -> Option<Summary> { /// any member registered), so joining works as in a full mesh. We always *answer* /// anyone who contacts us. pub(crate) fn is_neighbour(state: &AppState, peer: &str) -> bool { - match &*state - .neighbours - .read() - .unwrap_or_else(|poisoned| poisoned.into_inner()) - { + match &*state.neighbours.read_or_recover() { Some(view) => view.neighbours.contains(peer), None => true, } @@ -154,10 +149,7 @@ pub(crate) async fn refresh(state: &AppState) -> Option<NeighbourChange> { } } let previous = { - let view = state - .neighbours - .read() - .unwrap_or_else(|poisoned| poisoned.into_inner()); + let view = state.neighbours.read_or_recover(); if view.as_ref().is_some_and(|view| { view.members == current && view.k == k @@ -207,10 +199,7 @@ pub(crate) async fn refresh(state: &AppState) -> Option<NeighbourChange> { .iter() .filter(|m| m.as_str() != state.local_id() && !unreachable.contains(*m)) .count(); - *state - .neighbours - .write() - .unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(NeighbourView { + *state.neighbours.write_or_recover() = Some(NeighbourView { full_mesh: computed.len() >= reachable_others, members: current, k, |
