Try reserve port during startup

This commit is contained in:
Edward Shen 2021-07-22 13:37:32 -04:00
parent 0300135a6a
commit 8f5799211c
Signed by: edward
GPG key ID: F350507060ED6C90

View file

@ -87,7 +87,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Logging and warnings
//
// SimpleLogger::new().with_level(config.log_level).init()?;
tracing_subscriber::fmt()
.with_max_level(config.log_level)
.init();
@ -121,6 +120,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
// HTTP Server init
// Try bind to provided port first
let port_reservation = std::net::TcpListener::bind(bind_address);
if let Err(e) = port_reservation {
error!("Failed to bind to port!");
return Err(e.into());
};
let server = if OFFLINE_MODE.load(Ordering::Acquire) {
ServerState::init_offline()
} else {
@ -225,6 +231,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
})
.shutdown_timeout(60);
// drop port reservation, might have a TOCTOU but it's not a big deal; this
// is just a best effort.
std::mem::drop(port_reservation);
if disable_tls {
server.bind(bind_address)?.run().await?;
} else {