From aaf887a6fca200d253ce22f440596b0d5ec46f5e Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 5 Mar 2026 15:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96PointNewValue=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E5=A4=84=E7=90=86=EF=BC=9A=E5=B9=B6=E8=A1=8C=E6=89=A7?= =?UTF-8?q?=E8=A1=8Cupdate=5Fpoint=5Fmonitor=5Fdata=E5=92=8Csend=5Fto=5Fpu?= =?UTF-8?q?blic=EF=BC=8C=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84ws=5Fmanager=5Fclone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/event.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/event.rs b/src/event.rs index 565ce0f..10319b3 100644 --- a/src/event.rs +++ b/src/event.rs @@ -133,23 +133,32 @@ impl EventManager { value_changed, }; - // 只克隆一次 monitor,减少内存分配 - let monitor_clone = monitor.clone(); - if let Err(e) = connection_manager.update_point_monitor_data(monitor_clone).await { - tracing::error!("Failed to update point monitor data for point {}: {}", point_id, e); - } + // 克隆 monitor,用于并行执行 + let monitor_for_ws = monitor.clone(); + let monitor_for_db = monitor.clone(); - if let Some(ws_manager) = &ws_manager_clone { - let ws_message = crate::websocket::WsMessage::PointNewValue(monitor); - if let Err(e) = ws_manager.send_to_public(ws_message).await { - tracing::error!("Failed to send WebSocket message to public room: {}", e); + // 并行执行 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); } + }); - // 暂时注释掉 send_to_client,因为现在信息只需发送到 public - // if let Err(e) = ws_manager.send_to_client(point_id, ws_message).await { - // tracing::error!("Failed to send WebSocket message to client room {}: {}", point_id, e); - // } - } + tokio::spawn(async move { + // 发送WebSocket消息 + if let Some(ws_manager) = &ws_manager_clone { + let ws_message = crate::websocket::WsMessage::PointNewValue(monitor_for_ws); + if let Err(e) = ws_manager.send_to_public(ws_message).await { + tracing::error!("Failed to send WebSocket message to public room: {}", e); + } + + // 暂时注释掉 send_to_client,因为现在信息只需发送到 public + // if let Err(e) = ws_manager.send_to_client(point_id, ws_message).await { + // tracing::error!("Failed to send WebSocket message to client room {}: {}", point_id, e); + // } + } + }); } else { tracing::warn!("Point not found for source {} client_handle {}", source_id, client_handle); }