make process of starting listeners synchronous

Alternatively, a semaphore could have been used which might be a bit faster,
but since the start up process is only used once this simpler solution is
not at a significant disadvantage while being much simpler.
This commit is contained in:
Johann150 2021-10-22 14:21:40 +02:00
parent 4fee512843
commit 6af5efbf67
No known key found for this signature in database
GPG key ID: 9EE6577A2A06F8F1

View file

@ -51,24 +51,27 @@ fn main() {
// an error when trying to start
let mut listening_unspecified = false;
let handles = ARGS.addrs.iter().map(|addr| {
let mut handles = vec![];
for addr in &ARGS.addrs {
let arc = mimetypes.clone();
let was_listening_unspecified = listening_unspecified;
let handle = tokio::spawn(async move {
let listener = match TcpListener::bind(addr).await {
Err(e) => {
if !(addr.ip().is_unspecified() && was_listening_unspecified) {
panic!("Failed to listen on {}: {}", addr, e)
} else {
// already listening on the other unspecified address
log::warn!("Could not start listener on {}, but already listening on another unspecified address. Probably your system automatically listens in dual stack?", addr);
return;
}
}
Ok(listener) => listener,
};
let listener = match TcpListener::bind(addr).await {
Err(e) => {
if !(addr.ip().is_unspecified() && listening_unspecified) {
panic!("Failed to listen on {}: {}", addr, e)
} else {
// already listening on the other unspecified address
log::warn!("Could not start listener on {}, but already listening on another unspecified address. Probably your system automatically listens in dual stack?", addr);
continue;
}
}
Ok(listener) => listener,
};
listening_unspecified |= addr.ip().is_unspecified();
handles.push(tokio::spawn(async move {
log::info!("Started listener on {}", addr);
loop {
let (stream, _) = listener.accept().await.unwrap_or_else(|e| {
panic!("could not accept new connection on {}: {}", addr, e)
@ -86,10 +89,8 @@ fn main() {
}
});
}
});
listening_unspecified |= addr.ip().is_unspecified();
handle
});
}))
};
futures_util::future::join_all(handles).await;
});