Async function with tokio::main , await error handling advice
⚓ Rust 📅 2026-02-24 👤 surdeus 👁️ 1Hi,
I'm working on a project where i'm handling tokio and async. My code works, but i do need feel confident in the error handling i'm doing since it is my first time working with async in rust & with tokio since each function return is just made with .await? and i can't find good ressources to deepen my understanding in this.
Here is my main loop, run in my async fn with tokio::main
loop {
// in the main receiver we will keep it clean
// we need : cpu ram memory network disks ressources sending and caching
// it will be fixed with flags and config after but for now, just try to be exhaustive while keeping things apart
if collector_agent.debug_mode {
collector_agent.print_system_values();
collector_agent.print_networks_values();
collector_agent.print_disks_values();
collector_agent.refresh_all_metrics();
}
if collector_agent.is_working {
if collector_agent.config.resources.cpu {
communicator_agent
.send_cpu(collector_agent.system_agent.prepare_cpu_payload())
.await?;
}
if collector_agent.config.resources.disk {
communicator_agent
.send_disks(collector_agent.prepare_disks_payload())
.await?;
}
if collector_agent.config.resources.memory {
communicator_agent
.send_memory(collector_agent.system_agent.prepare_memory_payload())
.await?;
}
if collector_agent.config.resources.network {
communicator_agent
.send_network(collector_agent.prepare_network_payload())
.await?;
}
if collector_agent.config.resources.processes {
communicator_agent
.send_processes(collector_agent.system_agent.prepare_processes_payload())
.await?;
}
}
// we can switch it up, it's juste the minimum time interval to get a correct cpu evolution
// don't know where to put it rn
tokio::time::sleep(
sysinfo::MINIMUM_CPU_UPDATE_INTERVAL.max(Duration::from_millis(
collector_agent.config.general.refresh_rate_ms,
)),
)
.await;
// for debugging rn, just a little more delay to read, now parsed from config file
}
4 posts - 3 participants
🏷️ Rust_feed