chore: graceful exit on bind fail (#4882)

This commit is contained in:
discord9
2024-10-25 17:29:39 +08:00
committed by GitHub
parent 4f3afb13b6
commit 2485f66077
3 changed files with 16 additions and 6 deletions

View File

@@ -325,7 +325,7 @@ mod tests {
});
let addr = ([127, 0, 0, 1], port).into();
let server = Server::bind(&addr).serve(make_svc);
let server = Server::try_bind(&addr).unwrap().serve(make_svc);
let graceful = server.with_graceful_shutdown(async {
rx.await.ok();
});

View File

@@ -36,6 +36,15 @@ use crate::http::error_result::status_code_to_http_status;
#[snafu(visibility(pub))]
#[stack_trace_debug]
pub enum Error {
#[snafu(display("Failed to bind address: {}", addr))]
AddressBind {
addr: SocketAddr,
#[snafu(source)]
error: hyper::Error,
#[snafu(implicit)]
location: Location,
},
#[snafu(display("Arrow error"))]
Arrow {
#[snafu(source)]
@@ -565,9 +574,9 @@ impl ErrorExt for Error {
| Arrow { .. }
| FileWatch { .. } => StatusCode::Internal,
AlreadyStarted { .. } | InvalidPromRemoteReadQueryResult { .. } => {
StatusCode::IllegalState
}
AddressBind { .. }
| AlreadyStarted { .. }
| InvalidPromRemoteReadQueryResult { .. } => StatusCode::IllegalState,
UnsupportedDataType { .. } => StatusCode::Unsupported,

View File

@@ -53,7 +53,7 @@ use tower_http::trace::TraceLayer;
use self::authorize::AuthState;
use self::table_result::TableResponse;
use crate::configurator::ConfiguratorRef;
use crate::error::{AlreadyStartedSnafu, Error, HyperSnafu, Result, ToJsonSnafu};
use crate::error::{AddressBindSnafu, AlreadyStartedSnafu, Error, HyperSnafu, Result, ToJsonSnafu};
use crate::http::arrow_result::ArrowResponse;
use crate::http::csv_result::CsvResponse;
use crate::http::error_result::ErrorResponse;
@@ -933,7 +933,8 @@ impl Server for HttpServer {
app = configurator.config_http(app);
}
let app = self.build(app);
let server = axum::Server::bind(&listening)
let server = axum::Server::try_bind(&listening)
.with_context(|_| AddressBindSnafu { addr: listening })?
.tcp_nodelay(true)
// Enable TCP keepalive to close the dangling established connections.
// It's configured to let the keepalive probes first send after the connection sits