EmbarkStudios / server-framework

Framework for running network services, opinions included
Apache License 2.0
36 stars 4 forks source link

Health checks #12

Closed davidpdrsn closed 2 years ago

davidpdrsn commented 2 years ago

This basically just copy/pastes our internal health checks and converts the HealthCheck trait itself into an #[async_trait].

In our previous version we expose both /health/live and /health/ready which corresponds to livenessProbe and readinessProbe in Kubernetes. I guess that makes sense to keep but would like to hear what people think.

Example usage:

Server::new(config)
    .with(routes)
    .with_health_check(MyHealthCheck::default())
    .serve()
    .await
    .expect("server failed to start");

#[derive(Clone, Default)]
pub struct MyHealthCheck;

#[async_trait]
impl HealthCheck for MyHealthCheck {
    async fn is_live(&mut self) -> anyhow::Result<()> {
        Ok(())
    }

    async fn is_ready(&mut self) -> anyhow::Result<()> {
        Ok(())
    }
}

Health checks can also be combined using HealthCheck::and:

let health = postgres_health_check.and(redis_health_check);

Server::new(config)
    .with_health_check(health)
    .serve()
    .await;

Related Issues

Fixes https://github.com/EmbarkStudios/server-framework/issues/3