Finish removing potentially dangerous unwraps from the websocket system.

This commit is contained in:
Herbert Wolverson 2024-07-17 08:51:10 -05:00
parent a649ea8639
commit 11e8a8fb86
7 changed files with 151 additions and 129 deletions

View File

@ -18,12 +18,13 @@ pub(super) async fn cake_watcher(circuit_id: String, tx: tokio::sync::mpsc::Send
match get_raw_circuit_data(&circuit_id) {
lqos_bus::BusResponse::RawQueueData(Some(msg)) => {
let json = serde_json::to_string(&QueueStoreTransit::from(*msg)).unwrap();
if let Ok(json) = serde_json::to_string(&QueueStoreTransit::from(*msg)) {
let send_result = tx.send(json.to_string()).await;
if send_result.is_err() {
break;
}
}
}
_ => {}
}
}

View File

@ -32,10 +32,11 @@ pub(super) async fn circuit_watcher(circuit: String, tx: tokio::sync::mpsc::Send
devices: devices_for_circuit,
};
let message = serde_json::to_string(&result).unwrap();
if let Ok(message) = serde_json::to_string(&result) {
if let Err(_) = tx.send(message.to_string()).await {
log::info!("Channel is gone");
break;
}
}
}
}

View File

@ -9,13 +9,12 @@ use crate::throughput_tracker::flow_data::{ALL_FLOWS, FlowAnalysis, FlowbeeLocal
const FIVE_MINUTES_AS_NANOS: u64 = 300 * 1_000_000_000;
fn recent_flows_by_circuit(circuit_id: &str) -> Vec<(FlowbeeKeyTransit, FlowbeeLocalData, FlowAnalysis)> {
let device_reader = SHAPED_DEVICES.read().unwrap();
if let Ok(device_reader) = SHAPED_DEVICES.read() {
if let Ok(now) = time_since_boot() {
let now_as_nanos = Duration::from(now).as_nanos() as u64;
let five_minutes_ago = now_as_nanos - FIVE_MINUTES_AS_NANOS;
let all_flows = ALL_FLOWS.lock().unwrap();
if let Ok(all_flows) = ALL_FLOWS.lock() {
let result: Vec<(FlowbeeKeyTransit, FlowbeeLocalData, FlowAnalysis)> = all_flows
.iter()
.filter_map(|(key, (local, analysis))| {
@ -25,11 +24,11 @@ fn recent_flows_by_circuit(circuit_id: &str) -> Vec<(FlowbeeKeyTransit, FlowbeeL
}
// Don't show flows that don't belong to the circuit
let local_ip_str ; // Using late binding
let remote_ip_str ;
let device_name ;
let asn_name ;
let asn_country ;
let local_ip_str; // Using late binding
let remote_ip_str;
let device_name;
let asn_name;
let asn_country;
let local_ip = match key.local_ip.as_ip() {
IpAddr::V4(ip) => ip.to_ipv6_mapped(),
IpAddr::V6(ip) => ip,
@ -78,6 +77,8 @@ fn recent_flows_by_circuit(circuit_id: &str) -> Vec<(FlowbeeKeyTransit, FlowbeeL
.collect();
return result;
}
}
}
Vec::new()
}
@ -127,12 +128,13 @@ pub(super) async fn flows_by_circuit(circuit: String, tx: tokio::sync::mpsc::Sen
circuit_id: circuit.clone(),
flows,
};
let message = serde_json::to_string(&result).unwrap();
if let Ok(message) = serde_json::to_string(&result) {
if let Err(_) = tx.send(message).await {
log::info!("Channel is gone");
log::debug!("Channel is gone");
break;
}
}
}
ticker.tick().await;
}

View File

@ -24,8 +24,14 @@ pub(super) async fn ping_monitor(ip_addresses: Vec<(String, String)>, tx: tokio:
loop {
ticker.tick().await;
let client_v4 = Client::new(&Config::default()).unwrap();
let client_v6 = Client::new(&Config::builder().kind(ICMP::V6).build()).unwrap();
let client_v4 = Client::new(&Config::default());
let client_v6 = Client::new(&Config::builder().kind(ICMP::V6).build());
if client_v4.is_err() || client_v6.is_err() {
log::info!("Failed to create ping clients");
break;
}
let client_v4 = client_v4.unwrap();
let client_v6 = client_v6.unwrap();
let mut tasks = Vec::new();
for (ip, label) in ip_addresses.iter() {
@ -42,7 +48,7 @@ pub(super) async fn ping_monitor(ip_addresses: Vec<(String, String)>, tx: tokio:
// Use futures to join on all tasks
for task in tasks {
task.await.unwrap();
let _ = task.await;
}
// Notify the channel that we're still here - this is really
@ -51,12 +57,13 @@ pub(super) async fn ping_monitor(ip_addresses: Vec<(String, String)>, tx: tokio:
ip: "test".to_string(),
result: PingState::ChannelTest,
};
let message = serde_json::to_string(&channel_test).unwrap();
if let Ok(message) = serde_json::to_string(&channel_test) {
if let Err(_) = tx.send(message.to_string()).await {
log::info!("Channel is gone");
log::debug!("Channel is gone");
break;
}
}
}
}
async fn send_timeout(tx: tokio::sync::mpsc::Sender<String>, ip: String) {
@ -64,10 +71,11 @@ async fn send_timeout(tx: tokio::sync::mpsc::Sender<String>, ip: String) {
ip,
result: PingState::NoResponse,
};
let message = serde_json::to_string(&result).unwrap();
if let Ok(message) = serde_json::to_string(&result) {
if let Err(_) = tx.send(message.to_string()).await {
log::info!("Channel is gone");
}
}
}
async fn send_alive(tx: tokio::sync::mpsc::Sender<String>, ip: String, ping_time: Duration, label: String) {
@ -78,10 +86,11 @@ async fn send_alive(tx: tokio::sync::mpsc::Sender<String>, ip: String, ping_time
label,
},
};
let message = serde_json::to_string(&result).unwrap();
if let Ok(message) = serde_json::to_string(&result) {
if let Err(_) = tx.send(message.to_string()).await {
log::info!("Channel is gone");
}
}
}
async fn ping(client: Client, addr: IpAddr, tx: tokio::sync::mpsc::Sender<String>, label: String) {

View File

@ -10,8 +10,11 @@ pub async fn flow_count(channels: Arc<PubSub>) {
}
let active_flows = {
let lock = ALL_FLOWS.lock().unwrap();
if let Ok(lock) = ALL_FLOWS.lock() {
lock.len() as u64
} else {
0
}
};
let expired_flows = RECENT_FLOWS.len();
let active_flows = json!(

View File

@ -20,16 +20,18 @@ pub async fn network_tree(channels: Arc<PubSub>) {
return;
}
let data: Vec<(usize, NetworkJsonTransport)> = spawn_blocking(|| {
let net_json = NETWORK_JSON.read().unwrap();
if let Ok(data) = spawn_blocking(|| {
if let Ok(net_json) = NETWORK_JSON.read() {
net_json
.get_nodes_when_ready()
.iter()
.enumerate()
.map(|(i, n)| (i, n.clone_to_transit()))
.collect()
}).await.unwrap();
.collect::<Vec<(usize, NetworkJsonTransport)>>()
} else {
Vec::new()
}
}).await {
let message = json!(
{
"event": PublishedChannels::NetworkTree.to_string(),
@ -37,6 +39,7 @@ pub async fn network_tree(channels: Arc<PubSub>) {
}
).to_string();
channels.send(PublishedChannels::NetworkTree, message).await;
}
}
#[derive(Serialize)]

View File

@ -21,7 +21,7 @@ pub async fn tree_capacity(channels: Arc<PubSub>) {
}
let capacities: Vec<_> = {
let reader = NETWORK_JSON.read().unwrap();
if let Ok(reader) = NETWORK_JSON.read() {
reader.get_nodes_when_ready().iter().map(|node| {
let node = node.clone_to_transit();
let down = node.current_throughput.0 as f64 * 8.0 / 1_000_000.0;
@ -48,6 +48,9 @@ pub async fn tree_capacity(channels: Arc<PubSub>) {
median_rtt,
}
}).collect()
} else {
Vec::new()
}
};
let message = json!(