优化PointNewValue事件处理:并行执行update_point_monitor_data和send_to_public,移除不必要的ws_manager_clone

This commit is contained in:
caoqianming 2026-03-05 15:28:04 +08:00
parent ee3ee273b2
commit aaf887a6fc
1 changed files with 23 additions and 14 deletions

View File

@ -133,14 +133,22 @@ impl EventManager {
value_changed, value_changed,
}; };
// 只克隆一次 monitor减少内存分配 // 克隆 monitor用于并行执行
let monitor_clone = monitor.clone(); let monitor_for_ws = monitor.clone();
if let Err(e) = connection_manager.update_point_monitor_data(monitor_clone).await { let monitor_for_db = monitor.clone();
// 并行执行 update_point_monitor_data 和 send_to_public不等待完成
tokio::spawn(async move {
// 更新监控数据
if let Err(e) = connection_manager.update_point_monitor_data(monitor_for_db).await {
tracing::error!("Failed to update point monitor data for point {}: {}", point_id, e); tracing::error!("Failed to update point monitor data for point {}: {}", point_id, e);
} }
});
tokio::spawn(async move {
// 发送WebSocket消息
if let Some(ws_manager) = &ws_manager_clone { if let Some(ws_manager) = &ws_manager_clone {
let ws_message = crate::websocket::WsMessage::PointNewValue(monitor); let ws_message = crate::websocket::WsMessage::PointNewValue(monitor_for_ws);
if let Err(e) = ws_manager.send_to_public(ws_message).await { if let Err(e) = ws_manager.send_to_public(ws_message).await {
tracing::error!("Failed to send WebSocket message to public room: {}", e); tracing::error!("Failed to send WebSocket message to public room: {}", e);
} }
@ -150,6 +158,7 @@ impl EventManager {
// tracing::error!("Failed to send WebSocket message to client room {}: {}", point_id, e); // tracing::error!("Failed to send WebSocket message to client room {}: {}", point_id, e);
// } // }
} }
});
} else { } else {
tracing::warn!("Point not found for source {} client_handle {}", source_id, client_handle); tracing::warn!("Point not found for source {} client_handle {}", source_id, client_handle);
} }