Reduce Android and iOS background work

This commit is contained in:
shen
2026-12-03 16:46:29 -08:00
parent 9e05f1ad93
commit e68cb6c909
12 changed files with 189 additions and 60 deletions
+27 -1
View File
@@ -17,7 +17,7 @@ use tokio::runtime::Runtime;
static NEXT: AtomicU64 = AtomicU64::new(1);
/// Additive JSON operation set within ABI v1. Bump when hosts must rely on a
/// newly added operation or field; hosts reject libraries below what they need.
const CAPABILITY_REVISION: u32 = 2;
const CAPABILITY_REVISION: u32 = 3;
const OPERATIONS: &[&str] = &[
"abi_info",
"build_config",
@@ -27,6 +27,7 @@ const OPERATIONS: &[&str] = &[
"start_bridged",
"stop",
"status",
"health",
"wait_routes",
"socks_start",
"socks_stop",
@@ -374,6 +375,23 @@ fn dispatch(context: &Context, request: Value) -> anyhow::Result<Value> {
None => json!({"running": false, "active_sessions": 0}),
})
}
"health" => {
let engine_running = network.as_ref().is_some_and(|n| n.is_running());
let bridge_running = context
.socks
.lock()
.unwrap()
.as_ref()
.is_some_and(|bridge| bridge.snapshot().running);
let routes = network
.as_ref()
.map(|n| n.watch_mesh_routes().borrow().clone());
Ok(json!({
"engine_running": engine_running,
"mesh_routable": engine_running && bridge_running,
"routes": routes,
}))
}
"status" => match network.as_ref() {
Some(n) => {
let mut snapshot = context.runtime.block_on(n.snapshot());
@@ -769,6 +787,9 @@ mod tests {
call(id, r#"{"op":"status"}"#)["data"]["health"]["mesh_routable"],
false
);
let health = call(id, r#"{"op":"health"}"#);
assert_eq!(health["data"]["engine_running"], false);
assert_eq!(health["data"]["routes"], Value::Null);
// Freeing without stop also stops the network and wakes a waiter.
let started = call(
id,
@@ -776,6 +797,11 @@ mod tests {
);
assert_eq!(started["ok"], true, "{started}");
let generation = started["data"]["route_generation"].as_u64().unwrap();
let health = call(id, r#"{"op":"health"}"#);
assert_eq!(health["data"]["engine_running"], true);
assert_eq!(health["data"]["mesh_routable"], true);
assert_eq!(health["data"]["routes"]["generation"], generation);
assert!(health["data"].get("diagnostic_traces").is_none());
let waiter = std::thread::spawn(move || {
let at = std::time::Instant::now();
let reply = call(