added active app report throttling (500ms)

This commit is contained in:
2026-01-26 14:48:26 +08:00
parent 0b1686653c
commit 9662c1f66c

View File

@@ -1,12 +1,12 @@
use once_cell::sync::Lazy;
use rust_socketio::Payload; use rust_socketio::Payload;
use tauri::async_runtime; use tauri::async_runtime;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tokio::time::Duration;
use tracing::error; use tracing::error;
use crate::{ use crate::{lock_r, services::health_manager::show_health_manager_with_error, state::FDOLL};
lock_r,
services::health_manager::show_health_manager_with_error,
state::FDOLL,
};
use super::WS_EVENT; use super::WS_EVENT;
@@ -17,6 +17,9 @@ pub struct UserStatusPayload {
pub state: String, pub state: String,
} }
static USER_STATUS_REPORT_DEBOUNCE: Lazy<Mutex<Option<JoinHandle<()>>>> =
Lazy::new(|| Mutex::new(None));
pub async fn report_user_status(status: UserStatusPayload) { pub async fn report_user_status(status: UserStatusPayload) {
let payload_value = match serde_json::to_value(&status) { let payload_value = match serde_json::to_value(&status) {
Ok(val) => val, Ok(val) => val,
@@ -38,34 +41,49 @@ pub async fn report_user_status(status: UserStatusPayload) {
} }
}; };
if let Some(client) = client_opt { {
if !is_initialized { let mut debouncer = USER_STATUS_REPORT_DEBOUNCE.lock().await;
return; if let Some(handle) = debouncer.take() {
handle.abort();
} }
let payload_value_clone = payload_value.clone();
match async_runtime::spawn_blocking(move || { let client_opt_clone = client_opt.clone();
client.emit( let is_initialized_clone = is_initialized;
WS_EVENT::CLIENT_REPORT_USER_STATUS, let handle = tokio::spawn(async move {
Payload::Text(vec![payload_value]), tokio::time::sleep(Duration::from_millis(500)).await;
) if let Some(client) = client_opt_clone {
}) if !is_initialized_clone {
.await return;
{ }
Ok(Ok(_)) => (), match async_runtime::spawn_blocking(move || {
Ok(Err(e)) => { client.emit(
error!("Failed to emit user status report: {}", e); WS_EVENT::CLIENT_REPORT_USER_STATUS,
show_health_manager_with_error(Some(format!( Payload::Text(vec![payload_value_clone]),
"WebSocket emit failed: {}", )
e })
))); .await
{
Ok(Ok(_)) => (),
Ok(Err(e)) => {
error!("Failed to emit user status report: {}", e);
show_health_manager_with_error(Some(format!(
"WebSocket emit failed: {}",
e
)));
}
Err(e) => {
error!(
"Failed to execute blocking task for user status report: {}",
e
);
show_health_manager_with_error(Some(format!(
"WebSocket task failed: {}",
e
)));
}
}
} }
Err(e) => { });
error!("Failed to execute blocking task for user status report: {}", e); *debouncer = Some(handle);
show_health_manager_with_error(Some(format!(
"WebSocket task failed: {}",
e
)));
}
}
} }
} }