Skip to content

Commit

Permalink
chore: graceful exit on bind fail (#4882)
Browse files Browse the repository at this point in the history
  • Loading branch information
discord9 authored Oct 25, 2024
1 parent 4f3afb1 commit 2485f66
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/common/greptimedb-telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
15 changes: 12 additions & 3 deletions src/servers/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -565,9 +574,9 @@ impl ErrorExt for Error {
| Arrow { .. }
| FileWatch { .. } => StatusCode::Internal,

AlreadyStarted { .. } | InvalidPromRemoteReadQueryResult { .. } => {
StatusCode::IllegalState
}
AddressBind { .. }
| AlreadyStarted { .. }
| InvalidPromRemoteReadQueryResult { .. } => StatusCode::IllegalState,

UnsupportedDataType { .. } => StatusCode::Unsupported,

Expand Down
5 changes: 3 additions & 2 deletions src/servers/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2485f66

Please sign in to comment.