Skip to main content

HealthChecker

Struct HealthChecker 

Source
pub struct HealthChecker { /* private fields */ }
Expand description

Health checker that orchestrates multiple health checks with caching.

The HealthChecker runs registered health checks and caches the results for a configurable TTL to avoid excessive checking overhead. This is particularly important when health checks are expensive (e.g., database queries, external service calls).

§Thread Safety

The HealthChecker is thread-safe and can be shared across threads using Arc. The internal cache is protected by a RwLock for concurrent access.

§Caching Behavior

  • Health check results are cached for cache_ttl duration
  • Cached results are returned if still valid (not expired)
  • Expired results trigger a new health check execution
  • Cache updates are atomic and thread-safe

§Example

use elara_runtime::health::{HealthChecker, HealthCheck, HealthCheckResult};
use std::time::Duration;
use std::sync::Arc;

struct MyCheck;
impl HealthCheck for MyCheck {
    fn name(&self) -> &str { "my_check" }
    fn check(&self) -> HealthCheckResult { HealthCheckResult::Healthy }
}

let mut checker = HealthChecker::new(Duration::from_secs(30));
checker.add_check(Box::new(MyCheck));

// First call executes checks
let status1 = checker.check_health();

// Second call within TTL returns cached result
let status2 = checker.check_health();

Implementations§

Source§

impl HealthChecker

Source

pub fn new(cache_ttl: Duration) -> Self

Creates a new HealthChecker with the specified cache TTL.

§Arguments
  • cache_ttl - Duration for which health check results are cached
§Example
use elara_runtime::health::HealthChecker;
use std::time::Duration;

let checker = HealthChecker::new(Duration::from_secs(30));
Source

pub fn with_default_config() -> Self

Creates a new HealthChecker with default configuration.

Uses a default cache TTL of 30 seconds.

Source

pub fn with_config(config: HealthCheckerConfig) -> Self

Creates a new HealthChecker with the specified configuration.

Source

pub fn add_check(&mut self, check: Box<dyn HealthCheck>)

Adds a health check to the checker.

Health checks are executed in the order they are added.

§Arguments
  • check - Boxed health check implementation
§Example
use elara_runtime::health::{HealthChecker, HealthCheck, HealthCheckResult};
use std::time::Duration;

struct MyCheck;
impl HealthCheck for MyCheck {
    fn name(&self) -> &str { "my_check" }
    fn check(&self) -> HealthCheckResult { HealthCheckResult::Healthy }
}

let mut checker = HealthChecker::new(Duration::from_secs(30));
checker.add_check(Box::new(MyCheck));
Source

pub fn check_health(&self) -> HealthStatus

Checks the health of all registered checks.

This method returns cached results if they are still valid (within TTL). If the cache is expired or empty, it executes all health checks and updates the cache.

§Returns

HealthStatus containing the overall status and individual check results.

§Performance
  • Cached reads: O(1) with read lock
  • Cache miss: O(n) where n is the number of checks, with write lock
§Example
use elara_runtime::health::HealthChecker;
use std::time::Duration;

let checker = HealthChecker::new(Duration::from_secs(30));
let status = checker.check_health();

if status.is_healthy() {
    println!("All systems operational");
} else {
    println!("System degraded or unhealthy");
}
Source

pub fn clear_cache(&self)

Clears the cached health status, forcing the next check to execute.

This is useful for testing or when you need to force a fresh health check.

Source

pub fn check_count(&self) -> usize

Returns the number of registered health checks.

Source

pub fn cache_ttl(&self) -> Duration

Returns the cache TTL duration.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,