Skip to content

Commit

Permalink
chore: hard crash on fail to bind server (#1268)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
NathanFlurry committed Oct 29, 2024
1 parent 45f0247 commit e7da3c0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
13 changes: 11 additions & 2 deletions packages/common/api-helper/build/src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,19 @@ where
});

let addr = SocketAddr::from((host, port));
let server = Server::bind(&addr).serve(make_service);
let server = match Server::try_bind(&addr) {
Ok(x) => x,
Err(err) => {
tracing::error!(?host, ?port, ?err, "failed to bind api server");

// TODO: Find cleaner way of crashing entire program
// Hard crash program since a server failing to bind is critical
std::process::exit(1);
}
};

tracing::info!(?host, ?port, "server listening");
server.await?;
server.serve(make_service).await?;

Ok(())
}
13 changes: 12 additions & 1 deletion packages/common/health-checks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ pub async fn run_standalone(config: Config) -> GlobalResult<()> {
}
});

let server = Server::try_bind(&addr)?.serve(make_service);
let server = match Server::try_bind(&addr) {
Ok(x) => x,
Err(err) => {
tracing::error!(?host, ?port, ?err, "failed to bind health check server");

// TODO: Find cleaner way of crashing entire program
// Hard crash program since a server failing to bind is critical
std::process::exit(1);
}
};

let server = server.serve(make_service);

tracing::info!(?host, ?port, "started health server");
server.await?;
Expand Down
13 changes: 12 additions & 1 deletion packages/common/metrics/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ pub async fn run_standalone(config: rivet_config::Config) -> GlobalResult<()> {
let port = config.server()?.rivet.metrics.port();
let addr = SocketAddr::from((host, port));

let server = Server::try_bind(&addr)?.serve(make_service_fn(|_| async {
let server = match Server::try_bind(&addr) {
Ok(x) => x,
Err(err) => {
tracing::error!(?host, ?port, ?err, "failed to bind metrics server");

// TODO: Find cleaner way of crashing entire program
// Hard crash program since a server failing to bind is critical
std::process::exit(1);
}
};

let server = server.serve(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(serve_req))
}));

Expand Down
13 changes: 11 additions & 2 deletions packages/infra/pegboard/manager/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,17 @@ impl Ctx {
// Start runner socket
let self2 = self.clone();
let runner_socket = tokio::spawn(async move {
tracing::info!(port=%RUNNER_PORT, "listening for runner sockets");
let listener = TcpListener::bind(("0.0.0.0", RUNNER_PORT)).await?;
tracing::warn!(port=%RUNNER_PORT, "listening for runner sockets");
let listener = match TcpListener::bind(("0.0.0.0", RUNNER_PORT)).await {
Ok(x) => x,
Err(err) => {
tracing::error!(host = "0.0.0.0", port = ?RUNNER_PORT, "failed to bind pegboard manager server");

// TODO: Find cleaner way of crashing entire program
// Hard crash program since a server failing to bind is critical
std::process::exit(1);
}
};

loop {
use std::result::Result::{Err, Ok};
Expand Down
13 changes: 12 additions & 1 deletion packages/infra/pegboard/manager/src/metrics/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ const METRICS_PORT: u16 = 6000;
pub async fn run_standalone() {
let addr = SocketAddr::from(([0, 0, 0, 0], METRICS_PORT));

let server = Server::bind(&addr).serve(make_service_fn(|_| async {
let server = match Server::try_bind(&addr) {
Ok(x) => x,
Err(err) => {
tracing::error!(?host, ?port, ?err, "failed to bind pegboard metrics server");

// TODO: Find cleaner way of crashing entire program
// Hard crash program since a server failing to bind is critical
std::process::exit(1);
}
};

let server = server.serve(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(serve_req))
}));
if let Err(err) = server.await {
Expand Down
11 changes: 10 additions & 1 deletion packages/services/pegboard/standalone/ws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,16 @@ async fn socket_thread(ctx: &StandaloneCtx, conns: Arc<RwLock<Connections>>) ->
let port = ctx.config().server()?.rivet.pegboard.port();
let addr = SocketAddr::from((host, port));

let listener = TcpListener::bind(addr).await?;
let listener = match TcpListener::bind(addr).await {
Ok(x) => x,
Err(err) => {
tracing::error!(?addr, ?err, "failed to bind pegboard ws server");

// TODO: Find cleaner way of crashing entire program
// Hard crash program since a server failing to bind is critical
std::process::exit(1);
}
};
tracing::info!(?port, ?port, "server listening");

loop {
Expand Down

0 comments on commit e7da3c0

Please sign in to comment.