optimizations
This commit is contained in:
@@ -113,7 +113,10 @@ async fn websocket_connect(
|
||||
break;
|
||||
}
|
||||
Ok(_) = rx.changed() => {
|
||||
let value = rx.borrow_and_update().clone();
|
||||
let value = {
|
||||
let ref_val = rx.borrow_and_update();
|
||||
ref_val.clone()
|
||||
};
|
||||
let _ = tx.send(WebsocketResponse::TlmValue {
|
||||
uuid: uuid.clone(),
|
||||
value,
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt::Formatter;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
fn tlm_data_type_serialzier<S>(
|
||||
tlm_data_type: &TelemetryDataType,
|
||||
@@ -70,15 +70,15 @@ pub struct TelemetryData {
|
||||
}
|
||||
|
||||
pub struct TelemetryManagementService {
|
||||
uuid_mapping: Arc<Mutex<HashMap<String, String>>>,
|
||||
tlm_mapping: Arc<Mutex<HashMap<String, TelemetryData>>>,
|
||||
uuid_mapping: Arc<RwLock<HashMap<String, String>>>,
|
||||
tlm_mapping: Arc<RwLock<HashMap<String, TelemetryData>>>,
|
||||
}
|
||||
|
||||
impl TelemetryManagementService {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
uuid_mapping: Arc::new(Mutex::new(HashMap::new())),
|
||||
tlm_mapping: Arc::new(Mutex::new(HashMap::new())),
|
||||
uuid_mapping: Arc::new(RwLock::new(HashMap::new())),
|
||||
tlm_mapping: Arc::new(RwLock::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,10 +86,10 @@ impl TelemetryManagementService {
|
||||
&self,
|
||||
telemetry_definition_request: TelemetryDefinitionRequest,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let mut lock = self.uuid_mapping.lock().await;
|
||||
let lock = self.uuid_mapping.read().await;
|
||||
if let Some(uuid) = lock.get(&telemetry_definition_request.name) {
|
||||
trace!("Telemetry Definition Found {:?}", uuid);
|
||||
let tlm_lock = self.tlm_mapping.lock().await;
|
||||
let tlm_lock = self.tlm_mapping.read().await;
|
||||
if let Some(TelemetryData { definition, .. }) = tlm_lock.get(uuid) {
|
||||
if definition.data_type != telemetry_definition_request.data_type() {
|
||||
return Err("A telemetry item of the same name already exists".into());
|
||||
@@ -103,7 +103,9 @@ impl TelemetryManagementService {
|
||||
"Adding New Telemetry Definition {:?}",
|
||||
telemetry_definition_request
|
||||
);
|
||||
let mut tlm_lock = self.tlm_mapping.lock().await;
|
||||
drop(lock);
|
||||
let mut lock = self.uuid_mapping.write().await;
|
||||
let mut tlm_lock = self.tlm_mapping.write().await;
|
||||
let uuid = Uuid::random().value;
|
||||
lock.insert(telemetry_definition_request.name.clone(), uuid.clone());
|
||||
tlm_lock.insert(
|
||||
@@ -122,16 +124,13 @@ impl TelemetryManagementService {
|
||||
}
|
||||
|
||||
pub async fn get_by_name(&self, name: &String) -> Option<TelemetryData> {
|
||||
let uuid = {
|
||||
let uuid_lock = self.uuid_mapping.lock().await;
|
||||
uuid_lock.get(name).cloned()?
|
||||
};
|
||||
|
||||
self.get_by_uuid(&uuid).await
|
||||
let uuid_lock = self.uuid_mapping.read().await;
|
||||
let uuid = uuid_lock.get(name)?;
|
||||
self.get_by_uuid(uuid).await
|
||||
}
|
||||
|
||||
pub async fn get_by_uuid(&self, uuid: &String) -> Option<TelemetryData> {
|
||||
let tlm_lock = self.tlm_mapping.lock().await;
|
||||
let tlm_lock = self.tlm_mapping.read().await;
|
||||
tlm_lock.get(uuid).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user