Skip to content

Commit

Permalink
fix: add health-check
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed May 1, 2024
1 parent ba1a5b5 commit 07dc140
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions foundations/src/telementry/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async fn metrics(
}

#[cfg(feature = "health-check")]
pub use health_check::{register as register_health_check, unregister as unregister_health_check, HealthCheck};
pub use health_check::{register as register_health_check, unregister as unregister_health_check, HealthCheck, HealthCheckFn};

#[cfg(feature = "health-check")]
mod health_check {
Expand All @@ -170,17 +170,31 @@ mod health_check {
use futures::Future;
use scc::HashMap;

pub trait HealthCheck: Send + Sync + 'static {
fn check(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>;
}
pub struct HealthCheckFn<F>(F);

impl<F, Fut> HealthCheck for F
impl<F, Fut> HealthCheck for HealthCheckFn<F>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = bool> + Send + Sync + 'static,
Fut: Future<Output = bool> + Send + 'static,
{
fn check(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>> {
Box::pin((self)())
Box::pin((self.0)())
}
}

pub trait HealthCheck: Send + Sync + 'static {
fn check(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>;
}

impl<H: HealthCheck> HealthCheck for std::sync::Arc<H> {
fn check(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>> {
self.as_ref().check()
}
}

impl<H: HealthCheck> HealthCheck for Box<H> {
fn check(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>> {
self.as_ref().check()
}
}

Expand Down

0 comments on commit 07dc140

Please sign in to comment.