added active app report throttling (500ms)
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use rust_socketio::Payload;
|
||||
use tauri::async_runtime;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::Duration;
|
||||
use tracing::error;
|
||||
|
||||
use crate::{
|
||||
lock_r,
|
||||
services::health_manager::show_health_manager_with_error,
|
||||
state::FDOLL,
|
||||
};
|
||||
use crate::{lock_r, services::health_manager::show_health_manager_with_error, state::FDOLL};
|
||||
|
||||
use super::WS_EVENT;
|
||||
|
||||
@@ -17,6 +17,9 @@ pub struct UserStatusPayload {
|
||||
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) {
|
||||
let payload_value = match serde_json::to_value(&status) {
|
||||
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;
|
||||
}
|
||||
|
||||
match async_runtime::spawn_blocking(move || {
|
||||
client.emit(
|
||||
WS_EVENT::CLIENT_REPORT_USER_STATUS,
|
||||
Payload::Text(vec![payload_value]),
|
||||
Payload::Text(vec![payload_value_clone]),
|
||||
)
|
||||
})
|
||||
.await
|
||||
@@ -60,7 +72,10 @@ pub async fn report_user_status(status: UserStatusPayload) {
|
||||
)));
|
||||
}
|
||||
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!(
|
||||
"WebSocket task failed: {}",
|
||||
e
|
||||
@@ -68,4 +83,7 @@ pub async fn report_user_status(status: UserStatusPayload) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
*debouncer = Some(handle);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user