refactored mainloop

This commit is contained in:
Paul 2023-04-10 11:57:03 +02:00
parent f72c503e93
commit ae7738d45a

View File

@ -99,39 +99,49 @@ pub async fn run() {
_val = sleep_ms(200) => {}
};
let toblock;
{
let mut ctx = ctxarc.write().await;
ctx.gc_blocklist().await;
toblock = ctx.get_blocklist_toblock().await;
// apply firewall blocking
match fwblock(&toblock, &mut ret, &mut fwlen) {
Ok(_) => {}
Err(err) => {
println!("Err: {err}, unable to push firewall rules, use super user")
}
};
}
let ctxclone = Arc::clone(&ctxarc);
handle_fwblock(ctxclone, &mut ret, &mut fwlen).await;
// log lines
if ret.len() > 0 {
println!("{ret}", ret = ret.join(", "));
}
let ctxclone = Arc::clone(&ctxarc);
handle_cfg_reload(ctxclone, &mut last_cfg_reload).await;
}
}
async fn handle_cfg_reload(ctxclone: Arc<RwLock<Context>>, last_cfg_reload: &mut DateTime<Local>) {
let now_cfg_reload = Local::now().trunc_subsecs(0);
if (now_cfg_reload - last_cfg_reload) > Duration::seconds(5) {
if (now_cfg_reload - *last_cfg_reload) > Duration::seconds(5) {
// reload configuration from the server
let mut ctx = ctxclone.write().await;
match ctx.load().await {
Ok(_) => {
last_cfg_reload = Local::now().trunc_subsecs(0);
*last_cfg_reload = Local::now().trunc_subsecs(0);
}
Err(err) => {
println!("error loading config: {err}");
}
}
};
}
async fn handle_fwblock(ctxclone: Arc<RwLock<Context>>, ret: &mut Vec<String>, fwlen: &mut usize) {
let toblock;
{
let mut ctx = ctxclone.write().await;
ctx.gc_blocklist().await;
toblock = ctx.get_blocklist_toblock().await;
// apply firewall blocking
match fwblock(&toblock, ret, fwlen) {
Ok(_) => {}
Err(err) => {
println!("Err: {err}, unable to push firewall rules, use super user")
}
};
}
}