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,15 +41,24 @@ 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;
if let Some(handle) = debouncer.take() {
handle.abort();
}
let payload_value_clone = payload_value.clone();
let client_opt_clone = client_opt.clone();
let is_initialized_clone = is_initialized;
let handle = tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(500)).await;
if let Some(client) = client_opt_clone {
if !is_initialized_clone {
return; return;
} }
match async_runtime::spawn_blocking(move || { match async_runtime::spawn_blocking(move || {
client.emit( client.emit(
WS_EVENT::CLIENT_REPORT_USER_STATUS, WS_EVENT::CLIENT_REPORT_USER_STATUS,
Payload::Text(vec![payload_value]), Payload::Text(vec![payload_value_clone]),
) )
}) })
.await .await
@@ -60,7 +72,10 @@ pub async fn report_user_status(status: UserStatusPayload) {
))); )));
} }
Err(e) => { Err(e) => {
error!("Failed to execute blocking task for user status report: {}", e); error!(
"Failed to execute blocking task for user status report: {}",
e
);
show_health_manager_with_error(Some(format!( show_health_manager_with_error(Some(format!(
"WebSocket task failed: {}", "WebSocket task failed: {}",
e e
@@ -68,4 +83,7 @@ pub async fn report_user_status(status: UserStatusPayload) {
} }
} }
} }
});
*debouncer = Some(handle);
}
} }