From b197607d5f9e470164869698b4faa54c79081011 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 6 Mar 2026 09:22:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B7=BB=E5=8A=A0=20allocate=5Fcli?= =?UTF-8?q?ent=5Fhandle=20=E5=87=BD=E6=95=B0=E5=B0=81=E8=A3=85=E5=8F=A5?= =?UTF-8?q?=E6=9F=84=E5=88=86=E9=85=8D=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/connection.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 60aa344..d314322 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -717,6 +717,17 @@ impl ConnectionManager { None } + pub async fn allocate_client_handle(&self, source_id: Uuid) -> Option { + let mut status = self.status.write().await; + if let Some(conn) = status.get_mut(&source_id) { + let handle = conn.next_client_handle; + conn.next_client_handle += 1; + Some(handle) + } else { + None + } + } + pub async fn get_status(&self, source_id: Uuid) -> Option { let status = self.status.read().await; status.get(&source_id).map(ConnectionStatusView::from) @@ -1069,12 +1080,6 @@ impl ConnectionManager { } let subscription_id = subscription_id.unwrap_or_default(); - let mut client_handle_seed: u32 = self - .status - .read() - .await - .get(&source_id) - .map_or(1000, |s| s.next_client_handle); let mut items_to_create: Vec = Vec::new(); let mut item_points: Vec = Vec::new(); @@ -1082,8 +1087,8 @@ impl ConnectionManager { let node_id = NodeId::from_str(&p.external_id) .map_err(|e| format!("Invalid node id {}: {}", p.external_id, e))?; - let client_handle = client_handle_seed; - client_handle_seed += 1; + let client_handle = self.allocate_client_handle(source_id).await + .ok_or_else(|| format!("Failed to allocate client handle for source {}", source_id))?; if let Some(s) = self.status.write().await.get_mut(&source_id) { s.client_handle_map.insert(client_handle, p.point_id); @@ -1127,7 +1132,6 @@ impl ConnectionManager { conn_status .client_handle_map .retain(|_, point_id| !item_point_ids.contains(point_id)); - conn_status.next_client_handle = client_handle_seed; } let polled_count = self @@ -1183,7 +1187,6 @@ impl ConnectionManager { conn_status .client_handle_map .retain(|_, point_id| !failed_point_ids.contains(point_id)); - conn_status.next_client_handle = client_handle_seed; } let polled_count = self