Try reserve port during startup
This commit is contained in:
parent
0300135a6a
commit
8f5799211c
1 changed files with 11 additions and 1 deletions
12
src/main.rs
12
src/main.rs
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue