From 93a36cb1016915c687cd6ae84e76ab865c9295a9 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Sun, 31 May 2026 09:41:22 +0000 Subject: [PATCH] refactor: simplify client handling by removing Arc and Mutex wrappers from MasterConnectorTrait --- apps/nxmesh-agent/src/connector/master/mod.rs | 26 +++++-------------- apps/nxmesh-agent/src/connector/master/ssh.rs | 10 +++---- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/apps/nxmesh-agent/src/connector/master/mod.rs b/apps/nxmesh-agent/src/connector/master/mod.rs index 243dbc6..cf31b9e 100644 --- a/apps/nxmesh-agent/src/connector/master/mod.rs +++ b/apps/nxmesh-agent/src/connector/master/mod.rs @@ -13,7 +13,7 @@ pub trait MasterConnectorTrait: Send + Sync { &mut self, settings: &crate::config::settings::Settings, ) -> Result<(), Box>; - fn get_client(&self) -> Arc>; + fn get_client(&self) -> AgentClient; } pub struct MasterConnector { @@ -35,7 +35,7 @@ impl MasterConnectorTrait for MasterConnector { self.connector.connect(settings).await } - fn get_client(&self) -> Arc> { + fn get_client(&self) -> AgentClient { self.connector.get_client() } } @@ -58,7 +58,7 @@ mod tests { struct FakeConnector { called: Arc, fail: bool, - client: Arc>, + client: AgentClient, } #[async_trait::async_trait] @@ -74,7 +74,7 @@ mod tests { Ok(()) } - fn get_client(&self) -> Arc> { + fn get_client(&self) -> AgentClient { self.client.clone() } } @@ -93,10 +93,10 @@ mod tests { } } - fn test_client() -> Arc> { + fn test_client() -> AgentClient { let channel = tonic::transport::Channel::from_static("http://127.0.0.1:50051").connect_lazy(); - Arc::new(Mutex::new(AgentClient::new(channel))) + AgentClient::new(channel) } #[tokio::test] @@ -126,18 +126,4 @@ mod tests { let result = master.connect(&test_settings()).await; assert!(result.is_err()); } - - #[tokio::test] - async fn master_connector_returns_underlying_client() { - let shared_client = test_client(); - let fake = FakeConnector { - called: Arc::new(AtomicBool::new(false)), - fail: false, - client: shared_client.clone(), - }; - let master = MasterConnector::new(Box::new(fake)); - - let client = master.get_client(); - assert!(Arc::ptr_eq(&client, &shared_client)); - } } diff --git a/apps/nxmesh-agent/src/connector/master/ssh.rs b/apps/nxmesh-agent/src/connector/master/ssh.rs index 3245609..cdb1af9 100644 --- a/apps/nxmesh-agent/src/connector/master/ssh.rs +++ b/apps/nxmesh-agent/src/connector/master/ssh.rs @@ -1,6 +1,6 @@ -use std::{fs::File, io::Read, sync::Arc}; +use std::{fs::File, io::Read}; -use tokio::{fs::read, sync::Mutex}; +use tokio::fs::read; use nxmesh_proto::agent_service_client::AgentServiceClient; use tonic::transport::{Certificate, ClientTlsConfig, Identity}; @@ -11,7 +11,7 @@ use crate::config::settings::{MAuthSettings, TLSSettings}; use super::{AgentClient, MasterConnectorTrait}; pub struct SshMasterConnector { - client: Arc>, + client: AgentClient, } impl SshMasterConnector { @@ -34,7 +34,7 @@ impl SshMasterConnector { .connect_lazy(); // Create the gRPC client - let client = Arc::new(Mutex::new(AgentServiceClient::new(endpoint))); + let client = AgentServiceClient::new(endpoint); Ok(Self { client }) } @@ -126,7 +126,7 @@ impl MasterConnectorTrait for SshMasterConnector { Ok(()) } - fn get_client(&self) -> Arc> { + fn get_client(&self) -> AgentClient { self.client.clone() } }