Skip to content

Commit

Permalink
Revisit healthcheck error handling.
Browse files Browse the repository at this point in the history
Use enum variant and make all errors transparent to application.
  • Loading branch information
eloylp committed Apr 15, 2024
1 parent b256381 commit a541b94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
39 changes: 11 additions & 28 deletions ampd/src/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use error_stack::{Result, ResultExt};
use std::{fmt::Display, net::SocketAddrV4};
use error_stack::Result;
use std::{io, net::SocketAddrV4};
use thiserror::Error;
use tracing::info;

Expand All @@ -12,47 +12,30 @@ pub struct Server {
}

#[derive(Error, Debug)]
pub struct HealthCheckError {
msg: String,
}

impl HealthCheckError {
pub fn new(msg: String) -> Self {
Self { msg }
}
}

impl Display for HealthCheckError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.msg)
}
pub enum HealthCheckError {
#[error("Health check HTTP server problem: {0}")]
HTTPServerIO(#[from] io::Error),
}

impl Server {
pub async fn new(bind_addr: SocketAddrV4) -> Result<Self, HealthCheckError> {
Ok(Self {
listener: tokio::net::TcpListener::bind(bind_addr)
.await
.change_context(HealthCheckError::new(format!(
"Failed binding to addr: {}",
bind_addr
)))?,
.map_err(HealthCheckError::from)?,
})
}

pub async fn run(self, cancel: CancellationToken) -> Result<(), HealthCheckError> {
let app = Router::new().route("/status", get(status));
let bind_address = self
.listener
.local_addr()
.change_context(HealthCheckError::new(
"Failed getting local address".to_string(),
))?;
let bind_address = self.listener.local_addr().map_err(HealthCheckError::from)?;

info!("Starting health check server at: {}", bind_address);
axum::serve(self.listener, app)

Ok(axum::serve(self.listener, app)
.with_graceful_shutdown(async move { cancel.cancelled().await })
.await
.change_context(HealthCheckError::new("Failed executing server".to_string()))
.map_err(HealthCheckError::from)?)
}
}

Expand Down
8 changes: 4 additions & 4 deletions ampd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async fn prepare_app(cfg: Config, state: State) -> Result<App<impl Broadcaster>,

let health_check_server = health_check::Server::new(health_check_bind_addr)
.await
.change_context(Error::HealthCheckServerError)?;
.change_context(Error::HealthCheckError)?;

App::new(
tm_client,
Expand Down Expand Up @@ -421,7 +421,7 @@ where
.add_task(CancellableTask::create(|token| {
health_check_server
.run(token)
.change_context(Error::HealthCheckServerError)
.change_context(Error::HealthCheckError)
}))
.add_task(CancellableTask::create(|token| {
event_processor
Expand Down Expand Up @@ -473,6 +473,6 @@ pub enum Error {
BlockHeightMonitor,
#[error("invalid finalizer type for chain {0}")]
InvalidFinalizerType(ChainName),
#[error("Health check server error")]
HealthCheckServerError,
#[error("Health check system error")]
HealthCheckError,
}

0 comments on commit a541b94

Please sign in to comment.