diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index 88bbec7..12074f1 100644 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -14,13 +14,13 @@ echo -n "Checking if containers connected to each other... " for i in {2..12} do ip="172.17.0.${i}" - curl -s "${ip}:31372/memory" | grep -e true -e false -c | grep 12 > /dev/null || + curl -s "${ip}:31372/memory" | grep -e true -e false -c | grep 52 > /dev/null || echo Container at ip ${ip} did not connect to all other containers. done echo OK! echo -n "Checking if containers can sign data... " -for i in {2..12} +for i in {2..52} do ip="172.17.0.${i}" random_key=$(curl -s "${ip}:31372/memory" | grep true | tail -1 | awk '{ print $4 }') diff --git a/src/datastore.rs b/src/datastore.rs index d236c34..ee35284 100644 --- a/src/datastore.rs +++ b/src/datastore.rs @@ -268,10 +268,24 @@ impl Store { self.nodes.insert(ip, info.clone()) } - // pub async fn remove_node(&self, ip: &str) { - // let mut nodes = self.nodes.lock().await; - // nodes.remove(ip); - // } + pub async fn remove_inactive_nodes(&self) { + let mut dangling_pubkeys = Vec::new(); + self.nodes.retain(|_, v| { + let age = SystemTime::now() + .duration_since(v.updated_at) + .unwrap_or(Duration::ZERO) + .as_secs(); + if age > 120 { + dangling_pubkeys.push(v.pubkey.clone()); + false + } else { + true + } + }); + for pubkey in dangling_pubkeys.iter() { + self.keys.remove(pubkey); + } + } pub async fn get_localhost(&self) -> NodeUpdate { // TODO trigger reset_localhost_keys on error instead of expects diff --git a/src/main.rs b/src/main.rs index c343d91..49ab31d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ async fn cycle_keys(ds: Arc, tx: Sender) { loop { sleep(Duration::from_secs(60)).await; let _ = tx.send(ds.reset_localhost_keys().await); + ds.remove_inactive_nodes().await; } }