diff options
| -rw-r--r-- | src/swarm/bootstrap.rs | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/swarm/bootstrap.rs b/src/swarm/bootstrap.rs index b280b38..ed0c5cc 100644 --- a/src/swarm/bootstrap.rs +++ b/src/swarm/bootstrap.rs @@ -66,7 +66,7 @@ pub(crate) async fn bootstrap(state: &AppState, seeds: &[String], start: Start) state.mark_ready(swarm_id).await?; // Members of the swarm still running, if any: they merge with this node, or it // rejoins them if they moved on to a newer epoch. - resolve_seeds(state, &mut seeds.to_vec(), &mut HashSet::new()).await; + reach_seeds(state, seeds).await; return Ok(()); } if start == Start::Genesis { @@ -143,7 +143,7 @@ pub(crate) async fn bootstrap(state: &AppState, seeds: &[String], start: Start) state.initial_sync_done.store(true, Ordering::Release); state.mark_ready(swarm_id).await?; // Remembered members, if any, to find those still running. - resolve_seeds(state, &mut seeds.to_vec(), &mut HashSet::new()).await; + reach_seeds(state, seeds).await; return Ok(()); } @@ -495,6 +495,31 @@ fn load_backup(path: &Path, files: &Files) -> Result<Document> { /// Resolves the seeds in `unresolved`, says HELLO to each that resolves, and keeps the /// rest for later. Warns once per seed that fails (`failing` remembers which), and says /// when such a seed resolves after all. +/// Resolves `seeds`, those of a node ready at once, and says HELLO to each; those that +/// don't resolve yet (the node, or they, not reachable yet) are tried again in the +/// background, backing off as a joining node does, until they resolve or the node stops. +async fn reach_seeds(state: &AppState, seeds: &[String]) { + let mut unresolved = seeds.to_vec(); + let mut failing = HashSet::new(); + resolve_seeds(state, &mut unresolved, &mut failing).await; + if unresolved.is_empty() { + return; + } + let state = state.clone(); + let mut closing = state.closing.subscribe(); + tokio::spawn(async move { + let mut retry = RESOLVE_RETRY; + while !unresolved.is_empty() { + tokio::select! { + () = tokio::time::sleep(retry) => {} + _ = closing.wait_for(|closing| *closing) => return, + } + resolve_seeds(&state, &mut unresolved, &mut failing).await; + retry = (retry * 2).min(RESOLVE_RETRY_MAX); + } + }); +} + async fn resolve_seeds( state: &AppState, unresolved: &mut Vec<String>, |
