Skip to content

Commit

Permalink
Feat ping with node usage (#298)
Browse files Browse the repository at this point in the history
* refactor: move gateway http logic to separate part

* feat: collect node metrics to gateway services
  • Loading branch information
giangndm authored May 28, 2024
1 parent e856cb8 commit bcc13fa
Show file tree
Hide file tree
Showing 17 changed files with 618 additions and 285 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ We are actively refactoring entire media server and network stack with [sans-io-
| MoQ | Media-over-Quic ||
| Monitoring | Dashboard for monitoring ||
| Recording | Record stream ||
| Gateway | External gateway [RFC-0003](https://github.com/8xFF/rfcs/pull/3) | 🚧 |
| Gateway | External gateway [RFC-0003](https://github.com/8xFF/rfcs/pull/3) | 🚀 |
| Connector | External event handling ||

Status:
Expand Down
8 changes: 5 additions & 3 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ convert-enum = { workspace = true }
num_enum = { workspace = true }
derive_more = { workspace = true }
rcgen = { version = "0.13", optional = true }
maxminddb = "0.24.0"
maxminddb = { version = "0.24.0", optional = true }
sysinfo = { version = "0.30.12", optional = true }

[features]
default = ["gateway", "media", "connector", "cert_utils"]
gateway = ["media-server-gateway", "quinn_vnet"]
media = ["media-server-runner", "quinn_vnet"]
gateway = ["media-server-gateway", "quinn_vnet", "node_metrics", "maxminddb"]
media = ["media-server-runner", "quinn_vnet", "node_metrics"]
connector = ["quinn_vnet"]
cert_utils = ["rcgen", "rustls"]
quinn_vnet = ["rustls", "quinn"]
node_metrics = ["sysinfo"]
3 changes: 3 additions & 0 deletions bin/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
pub enum MediaServerError {
GatewayRpcError = 0x00020001,
InvalidConnId = 0x00020002,
NodePoolEmpty = 0x00020003,
MediaResError = 0x00020004,
NotImplemented = 0x00020005,
}
2 changes: 2 additions & 0 deletions bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use atm0s_sdn::{NodeAddr, NodeId};

mod errors;
mod http;
#[cfg(feature = "node_metrics")]
mod node_metrics;
#[cfg(feature = "quinn_vnet")]
mod quinn;
pub mod server;
Expand Down
67 changes: 67 additions & 0 deletions bin/src/node_metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{
sync::mpsc::{channel, Receiver},
time::Duration,
};

use media_server_gateway::NodeMetrics;
use sans_io_runtime::ErrorDebugger2;
use sysinfo::{Disks, System};

const REFRESH_INTERVAL_SECONDS: u64 = 2;

pub struct NodeMetricsCollector {
rx: Receiver<NodeMetrics>,
}

impl Default for NodeMetricsCollector {
fn default() -> Self {
let (tx, rx) = channel();
let mut sys = System::new_all();
let mut disks = Disks::new();

disks.refresh_list();
sys.refresh_all();
sys.refresh_cpu();

std::thread::spawn(move || {
loop {
disks.refresh();
sys.refresh_all();
sys.refresh_cpu();

let mut sum = 0.0;
for cpu in sys.cpus() {
sum += cpu.cpu_usage();
}

let mut disk_used = 0;
let mut disk_sum = 0;
for disk in disks.iter() {
disk_sum += disk.total_space();
disk_used += disk.total_space() - disk.available_space();
}

tx.send(NodeMetrics {
cpu: (sum as usize / sys.cpus().len()) as u8,
memory: (100 * sys.used_memory() / sys.total_memory()) as u8,
disk: (100 * disk_used / disk_sum) as u8,
})
.print_err2("Collect node metrics error");

// Sleeping to let time for the system to run for long
// enough to have useful information.
std::thread::sleep(Duration::from_secs(REFRESH_INTERVAL_SECONDS));
}
});

Self { rx }
}
}

impl NodeMetricsCollector {
/// Only return data in each interval, if not return None.
/// Node that this method must node blocking thread
pub fn pop_measure(&mut self) -> Option<NodeMetrics> {
self.rx.try_recv().ok()
}
}
Loading

0 comments on commit bcc13fa

Please sign in to comment.