Expand description
Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
This file is part of Ri. The Ri project belongs to the Dunimd Team.
Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
§Health Check Module
This module provides health checking functionality for the Ri service mesh. It allows monitoring the health of services using various protocols and provides comprehensive health status information.
§Key Components
- RiHealthCheckConfig: Configuration for health checks
- RiHealthCheckResult: Result of a health check
- RiHealthCheckType: Supported health check types
- RiHealthCheckProvider: Trait for implementing health check providers
- RiHttpHealthCheckProvider: HTTP health check implementation
- RiTcpHealthCheckProvider: TCP health check implementation
- RiHealthChecker: Main health checking service
- RiHealthStatus: Health status enum
- RiHealthSummary: Summary of health check results
§Design Principles
- Protocol Agnostic: Supports multiple health check protocols (HTTP, TCP, gRPC, custom)
- Async-First: All health check operations are asynchronous
- Extensible: Easy to implement new health check providers
- Configurable: Highly configurable health check parameters
- Real-time Monitoring: Background tasks for continuous health monitoring
- Comprehensive Results: Detailed health check results with response times and error messages
- Health Summary: Aggregated health status with success rates and average response times
- Thread-safe: Uses Arc and RwLock for safe concurrent access
- Graceful Shutdown: Proper cleanup of background tasks
- Error Handling: Comprehensive error handling with RiResult
§Usage
use ri::prelude::*;
use std::time::Duration;
async fn example() -> RiResult<()> {
// Create a health checker with 30-second intervals
let health_checker = RiHealthChecker::new(Duration::from_secs(30));
// Register a health check for a service
let config = RiHealthCheckConfig {
endpoint: "/health".to_string(),
method: "GET".to_string(),
timeout: Duration::from_secs(5),
expected_status_code: 200,
expected_response_body: None,
headers: HashMap::new(),
};
health_checker.register_health_check(
"example-service",
"http://localhost:8080",
RiHealthCheckType::Http,
config
).await?;
// Start background health checks
health_checker.start_health_check("example-service", "http://localhost:8080").await?;
// Get health summary
let summary = health_checker.get_service_health_summary("example-service").await?;
println!("Service health: {:?}", summary.overall_status);
println!("Success rate: {:.2}%", summary.success_rate);
Ok(())
}Structs§
- RiGrpc
Health Check Provider - gRPC health check provider.
- RiHealth
Check Config - Configuration for health checks.
- RiHealth
Check Result - Result of a health check operation.
- RiHealth
Checker - Main health checker service.
- RiHealth
Summary - Summary of health check results.
- RiHttp
Health Check Provider - HTTP health check provider.
- RiTcp
Health Check Provider - TCP health check provider.
Enums§
- RiHealth
Check Type - Types of health checks supported.
- RiHealth
Status - Health status enum.
Traits§
- RiHealth
Check Provider - Trait for implementing health check providers.