revert ts change now that i have the GOAT lda-performance

This commit is contained in:
lda
2025-12-09 21:32:49 +07:00 Unverified
parent 940614e14a
commit 3f19635dfc
3 changed files with 98 additions and 19 deletions
+2 -2
View File
@@ -89,7 +89,7 @@ pub enum NUDState {
impl NUDState {
/// Argument form expected by `ip neigh ... nud <state>` (lowercase)
pub const fn _as_ip_neigh_arg(self) -> &'static str {
pub const fn as_ip_neigh_arg(self) -> &'static str {
match self {
NUDState::Permanent => "permanent",
NUDState::Reachable => "reachable",
@@ -183,7 +183,7 @@ impl IpNeighLine {
Self { state, ..self }
}
*/
pub fn _with_dev(dev: impl Into<String>) -> impl FnMut(Self) -> Self {
pub fn with_dev(dev: impl Into<String>) -> impl FnMut(Self) -> Self {
let dev = dev.into();
move |self_| Self {
dev: Some(dev.clone()),
+48 -7
View File
@@ -1,16 +1,57 @@
use lda_ipjs::subcommands::address::json as ipjs_json;
use std::collections::HashSet;
// /// 50ms
// pub async fn get_dev() -> HashSet<String> {
// use lda_ipjs::subcommands::address::json as ipjs_json;
// let mut devs: HashSet<String> = HashSet::new();
// if let Ok(items) = ipjs_json::get(None).await {
// for item in items {
// if item.ifname != "lo" && !item.ifname.is_empty() {
// devs.insert(item.ifname);
// }
// }
// }
// devs
// }
/// 3ms
pub async fn get_dev() -> HashSet<String> {
let mut devs: HashSet<String> = HashSet::new();
if let Ok(items) = ipjs_json::get(None).await {
for item in items {
if item.ifname != "lo" && !item.ifname.is_empty() {
devs.insert(item.ifname);
use std::fs;
fn get_dev() -> HashSet<String> {
let mut devs: HashSet<String> = HashSet::new();
if let Ok(rd) = std::fs::read_dir("/sys/class/net") {
for e in rd.flatten() {
if e.file_type()
.map(|ft| {
if ft.is_symlink() {
// true
fs::metadata(e.path()).map(|m| m.is_dir()).unwrap_or(false)
} else {
ft.is_dir()
}
})
.unwrap_or(false)
&& let Ok(name) = e.file_name().into_string()
&& name != "lo"
&& !name.is_empty()
{
devs.insert(name);
}
}
} else if let Ok(txt) = std::fs::read_to_string("/proc/net/dev") {
for line in txt.lines().skip(2) {
if let Some((name, _rest)) = line.split_once(':') {
let n = name.trim().to_string();
if n != "lo" && !n.is_empty() {
devs.insert(n);
}
}
}
}
devs
}
devs
tokio::task::spawn_blocking(get_dev)
.await
.unwrap_or_default()
}
pub async fn devs_sorted() -> Vec<String> {
+48 -10
View File
@@ -1,8 +1,7 @@
use macaddr::MacAddr;
use crate::arpparse::{IpNeighLine, NUDState};
use anyhow::{Context, Result};
use lda_ipjs::subcommands::neighbor as ipjs_neigh;
use crate::arpparse::{self, IpNeighLine, NUDState};
use anyhow::{Context, Result, bail};
use std::collections::HashSet;
use std::net::IpAddr;
@@ -95,21 +94,60 @@ pub async fn get_macs(
Ok(ip_filtered)
}
// /// the atomic get_macs. handle ONE thing only.
// pub async fn get_mac(
// ip: Option<IpAddr>,
// dev: Option<&str>,
// state: &[NUDState],
// ) -> Result<Vec<IpNeighLine>> {
// use lda_ipjs::subcommands::neighbor as ipjs_neigh;
// let ipjs_states: Vec<ipjs_neigh::NUDState> = state.iter().copied().map(Into::into).collect();
// let items = ipjs_neigh::json::get(ip, dev, &ipjs_states)
// .await
// .context("Calling ip -j neigh failed")?;
// let lines = items.into_iter().map(Into::into).collect();
// Ok(lines)
// }
/// the atomic get_macs. handle ONE thing only.
pub async fn get_mac(
ip: Option<IpAddr>,
dev: Option<&str>,
state: &[NUDState],
) -> Result<Vec<IpNeighLine>> {
let ipjs_states: Vec<ipjs_neigh::NUDState> = state.iter().copied().map(Into::into).collect();
let mut args: Vec<String> = vec!["neigh".into(), "show".into()];
if let Some(ip) = ip {
args.push("to".into());
args.push(ip.to_string());
}
if let Some(d) = dev {
args.push("dev".into());
args.push(d.to_string());
}
for nud in state {
args.push("nud".into());
args.push(nud.as_ip_neigh_arg().into());
}
let cmd = "ip";
let mut u = tokio::process::Command::new(cmd);
u.args(args);
let out = u.output().await?;
let items = ipjs_neigh::json::get(ip, dev, &ipjs_states)
.await
.context("Calling ip -j neigh failed")?;
if !out.status.success() {
bail!(String::from_utf8_lossy(&out.stderr).into_owned());
}
let lines = items.into_iter().map(Into::into).collect();
Ok(lines)
let lines = String::from_utf8_lossy(&out.stdout);
let parsed = lines.lines().flat_map(arpparse::parse_ip_neigh_line);
let rows: Vec<IpNeighLine> = if let Some(d) = dev {
parsed.map(IpNeighLine::with_dev(d)).collect()
} else {
parsed.collect()
};
Ok(rows)
}
/*