refactor: simplify client handling by removing Arc and Mutex wrappers from MasterConnectorTrait
This commit is contained in:
@@ -13,7 +13,7 @@ pub trait MasterConnectorTrait: Send + Sync {
|
||||
&mut self,
|
||||
settings: &crate::config::settings::Settings,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||
fn get_client(&self) -> Arc<Mutex<AgentClient>>;
|
||||
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<Mutex<AgentClient>> {
|
||||
fn get_client(&self) -> AgentClient {
|
||||
self.connector.get_client()
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ mod tests {
|
||||
struct FakeConnector {
|
||||
called: Arc<AtomicBool>,
|
||||
fail: bool,
|
||||
client: Arc<Mutex<AgentClient>>,
|
||||
client: AgentClient,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
@@ -74,7 +74,7 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_client(&self) -> Arc<Mutex<AgentClient>> {
|
||||
fn get_client(&self) -> AgentClient {
|
||||
self.client.clone()
|
||||
}
|
||||
}
|
||||
@@ -93,10 +93,10 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_client() -> Arc<Mutex<AgentClient>> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Mutex<AgentClient>>,
|
||||
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<Mutex<AgentClient>> {
|
||||
fn get_client(&self) -> AgentClient {
|
||||
self.client.clone()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user