Fix peer list persistence: do not forget previous peers

This commit is contained in:
Alex 2021-11-03 17:34:44 +01:00
parent bef6d627b0
commit 8c4f418fe8
No known key found for this signature in database
GPG key ID: EDABF9711E244EB1

View file

@ -500,13 +500,7 @@ impl System {
} }
} }
let peer_list = self if let Err(e) = self.save_peer_list().await {
.fullmesh
.get_peer_list()
.iter()
.map(|n| (n.id.into(), n.addr))
.collect::<Vec<_>>();
if let Err(e) = self.persist_peer_list.save_async(&peer_list).await {
warn!("Could not save peer list to file: {}", e); warn!("Could not save peer list to file: {}", e);
} }
@ -520,6 +514,28 @@ impl System {
} }
} }
async fn save_peer_list(&self) -> Result<(), Error> {
// Prepare new peer list to save to file
// It is a vec of tuples (node ID as Uuid, node SocketAddr)
let mut peer_list = self
.fullmesh
.get_peer_list()
.iter()
.map(|n| (n.id.into(), n.addr))
.collect::<Vec<_>>();
// Before doing it, we read the current peer list file (if it exists)
// and append it to the list we are about to save,
// so that no peer ID gets lost in the process.
if let Ok(mut prev_peer_list) = self.persist_peer_list.load_async().await {
prev_peer_list.retain(|(id, _ip)| peer_list.iter().all(|(id2, _ip2)| id2 != id));
peer_list.extend(prev_peer_list);
}
// Save new peer list to file
self.persist_peer_list.save_async(&peer_list).await
}
async fn pull_config(self: Arc<Self>, peer: Uuid) { async fn pull_config(self: Arc<Self>, peer: Uuid) {
let resp = self let resp = self
.rpc .rpc