single thread tokio for daemon

This commit is contained in:
Robin Appelman 2025-11-12 23:12:25 +01:00
commit 695f1e1d74
3 changed files with 12 additions and 15 deletions

View file

@ -5,7 +5,7 @@ edition = "2024"
rust-version = "1.87.0"
[dependencies]
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread", "signal", "net", "io-util"] }
tokio = { version = "1.48.0", features = ["macros", "rt", "signal", "net", "io-util"] }
tokio-stream = { version = "0.1.17", features = ["signal", "net"] }
toml = "0.9.8"
serde = { version = "1.0.228", features = ["derive"] }

View file

@ -9,14 +9,14 @@ use sd_notify::{NotifyState, notify};
use std::io::Error as IoError;
use std::pin::pin;
use thiserror::Error;
use tokio::runtime::Runtime;
use tokio::runtime::Builder;
use tokio::signal::ctrl_c;
use tokio::signal::unix::{SignalKind, signal};
use tokio_stream::wrappers::SignalStream;
use tracing::{debug, error, info};
pub fn daemon(config: Config) -> MainResult {
let rt = Runtime::new()?;
let rt = Builder::new_current_thread().enable_io().build()?;
Ok(rt.block_on(daemon_async(config))?)
}
@ -76,11 +76,7 @@ async fn daemon_async(mut config: Config) -> Result<(), DaemonError> {
for namespace in &state.namespaces {
println!("{}:", namespace.name());
for proxy in &namespace.proxies {
println!(
" {} => {}",
proxy.source,
proxy.destination,
);
println!(" {} => {}", proxy.source, proxy.destination,);
}
}
}
@ -105,12 +101,12 @@ struct State {
impl State {
pub fn new() -> Result<Self, DaemonError> {
let namespaces = NetNs::existing()?.map(ActiveNamespace::new).collect::<Result<Vec<_>, _>>()?;
Ok(State {
namespaces
})
let namespaces = NetNs::existing()?
.map(ActiveNamespace::new)
.collect::<Result<Vec<_>, _>>()?;
Ok(State { namespaces })
}
pub fn update(&mut self, config: &Config) -> Result<(), DaemonError> {
for removed in self.namespaces.extract_if(.., |namespace| {
config.get_namespace(namespace.name()).is_none()
@ -120,7 +116,8 @@ impl State {
for new in &config.namespaces {
if !self.has_namespace(&new.name) {
self.namespaces.push(ActiveNamespace::new(new.name.clone())?);
self.namespaces
.push(ActiveNamespace::new(new.name.clone())?);
}
}

View file

@ -61,7 +61,7 @@ impl Proxy {
})
}
pub async fn run(self, target: ForwardTarget, abort: AbortRegistration,) {
pub async fn run(self, target: ForwardTarget, abort: AbortRegistration) {
match self.socket {
ProxyListener::Tcp(socket) => {
run_tcp(socket, target.addr, abort).await