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_ttlduration - 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
impl HealthChecker
Sourcepub fn with_default_config() -> Self
pub fn with_default_config() -> Self
Creates a new HealthChecker with default configuration.
Uses a default cache TTL of 30 seconds.
Sourcepub fn with_config(config: HealthCheckerConfig) -> Self
pub fn with_config(config: HealthCheckerConfig) -> Self
Creates a new HealthChecker with the specified configuration.
Sourcepub fn add_check(&mut self, check: Box<dyn HealthCheck>)
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));Sourcepub fn check_health(&self) -> HealthStatus
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");
}Sourcepub fn clear_cache(&self)
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.
Sourcepub fn check_count(&self) -> usize
pub fn check_count(&self) -> usize
Returns the number of registered health checks.
Auto Trait Implementations§
impl Freeze for HealthChecker
impl !RefUnwindSafe for HealthChecker
impl Send for HealthChecker
impl Sync for HealthChecker
impl Unpin for HealthChecker
impl UnsafeUnpin for HealthChecker
impl !UnwindSafe for HealthChecker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request