improvements

This commit is contained in:
chayleaf 2024-08-09 16:43:04 +07:00
parent c8735ef768
commit 460cbdd06e
Signed by: chayleaf
GPG key ID: 78171AD46227E68E

View file

@ -368,9 +368,9 @@ impl UnboundMod for ExampleMod {
Err(err) => ret.report("ips", err), Err(err) => ret.report("ips", err),
} }
} }
for domain in v_domains.iter() { for rev_domain in v_domains.iter() {
ret.cache4.get_maybe_update_rev( ret.cache4.get_maybe_update_rev(
domain rev_domain
.iter() .iter()
.map(|x| x.as_slice()) .map(|x| x.as_slice())
.collect::<Vec<_>>() .collect::<Vec<_>>()
@ -384,7 +384,7 @@ impl UnboundMod for ExampleMod {
}, },
); );
ret.cache6.get_maybe_update_rev( ret.cache6.get_maybe_update_rev(
domain rev_domain
.iter() .iter()
.map(|x| x.as_slice()) .map(|x| x.as_slice())
.collect::<Vec<_>>() .collect::<Vec<_>>()
@ -419,7 +419,6 @@ impl UnboundMod for ExampleMod {
match rx.recv() { match rx.recv() {
Ok(val) => { Ok(val) => {
queue_start = Instant::now(); queue_start = Instant::now();
len += val.1.len();
Some(val) Some(val)
} }
Err(RecvError) => break, Err(RecvError) => break,
@ -427,20 +426,32 @@ impl UnboundMod for ExampleMod {
} else { } else {
match rx.recv_timeout((queue_start + Duration::from_secs(30)) - Instant::now()) match rx.recv_timeout((queue_start + Duration::from_secs(30)) - Instant::now())
{ {
Ok(val) => { Ok(val) => Some(val),
len += val.1.len();
Some(val)
}
Err(mpsc::RecvTimeoutError::Timeout) => None, Err(mpsc::RecvTimeoutError::Timeout) => None,
Err(mpsc::RecvTimeoutError::Disconnected) => break, Err(mpsc::RecvTimeoutError::Disconnected) => break,
} }
}; };
let do_it = res.is_none() let do_it =
|| len >= 128 res.is_none() || (Instant::now() - queue_start) > Duration::from_secs(25);
|| (Instant::now() - queue_start) > Duration::from_secs(25); if let Some((rulesets1, ips)) = res {
if let Some((rulesets, ips)) = res { for ruleset in rulesets1 {
for ruleset in rulesets { for ip1 in ips.iter().copied() {
bufs[ruleset].extend(ips.iter().copied()); match ip1 {
IpNet::V4(ip) => {
if !rulesets[ruleset].ips4.contains(&ip) {
rulesets[ruleset].ips4.insert(ip.into());
bufs[ruleset].push(ip1);
}
}
IpNet::V6(ip) => {
if !rulesets[ruleset].ips6.contains(&ip) {
rulesets[ruleset].ips6.insert(ip.into());
bufs[ruleset].push(ip1);
len += 1;
}
}
}
}
} }
} }
struct FlushSetMsg<'a, T> { struct FlushSetMsg<'a, T> {
@ -463,7 +474,7 @@ impl UnboundMod for ExampleMod {
nftnl_sys::nftnl_set_elems_nlmsg_build_payload(header, self.set.as_ptr()); nftnl_sys::nftnl_set_elems_nlmsg_build_payload(header, self.set.as_ptr());
} }
} }
if do_it { if do_it || len >= 128 {
let mut batch = nftnl::Batch::new(); let mut batch = nftnl::Batch::new();
for (ruleset, buf) in rulesets.iter().zip(bufs.iter_mut()) { for (ruleset, buf) in rulesets.iter().zip(bufs.iter_mut()) {
// internally represented as a range // internally represented as a range
@ -663,7 +674,6 @@ impl UnboundMod for ExampleMod {
let entry = rrset.entry(); let entry = rrset.entry();
let d = entry.data(); let d = entry.data();
let rk = rrset.rk(); let rk = rrset.rk();
// IN
if rk.rrset_class() != rr_class::IN { if rk.rrset_class() != rr_class::IN {
continue; continue;
} }
@ -684,15 +694,16 @@ impl UnboundMod for ExampleMod {
} }
} }
if !ip4.is_empty() || !ip6.is_empty() { if !ip4.is_empty() || !ip6.is_empty() {
let domain = match String::from_utf8( let domain = match split_rev_domain
split_rev_domain
.iter() .iter()
.rev() .rev()
.map(|x| x.as_slice()) .map(|x| String::from_utf8(x.to_vec()).map(|x| x + "."))
.collect::<Vec<_>>() .collect::<Result<String, _>>()
.join(&b"."[..]), {
) { Ok(mut x) => {
Ok(x) => x, x.pop();
x
}
Err(err) => { Err(err) => {
self.report("domain utf-8", err); self.report("domain utf-8", err);
return; return;