|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
Monitors connection health with heartbeat mechanism. More...
#include <health_monitor.h>


Public Member Functions | |
| health_monitor (std::chrono::seconds heartbeat_interval=std::chrono::seconds(30), size_t max_missed_heartbeats=3) | |
| Constructs a health monitor. | |
| ~health_monitor () noexcept | |
| Destructor - stops monitoring if active. | |
| auto | start_monitoring (std::shared_ptr< core::messaging_client > client) -> void |
| Starts monitoring the given client. | |
| auto | stop_monitoring () -> void |
| Stops monitoring. | |
| auto | get_health () const -> connection_health |
| Gets current health status. | |
| auto | set_health_callback (std::function< void(const connection_health &)> callback) -> void |
| Sets callback for health status changes. | |
| auto | is_monitoring () const noexcept -> bool |
| Checks if monitoring is active. | |
Private Member Functions | |
| auto | do_heartbeat () -> void |
| Performs a single heartbeat check. | |
| auto | schedule_next_heartbeat () -> void |
| Schedules the next heartbeat. | |
| auto | update_health (bool success, std::chrono::milliseconds response_time) -> void |
| Updates health metrics. | |
Private Attributes | |
| std::shared_ptr< core::messaging_client > | client_ |
| std::unique_ptr< asio::io_context > | io_context_ |
| std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > | work_guard_ |
| std::unique_ptr< asio::steady_timer > | heartbeat_timer_ |
| std::future< void > | io_future_ |
| std::chrono::seconds | heartbeat_interval_ |
| size_t | max_missed_heartbeats_ |
| std::mutex | health_mutex_ |
| connection_health | health_ |
| std::atomic< bool > | is_monitoring_ {false} |
| std::atomic< size_t > | total_heartbeats_ {0} |
| std::atomic< size_t > | failed_heartbeats_ {0} |
| std::function< void(const connection_health &)> | health_callback_ |
Monitors connection health with heartbeat mechanism.
Definition at line 74 of file health_monitor.h.
|
explicit |
Constructs a health monitor.
| heartbeat_interval | Interval between heartbeat checks (default: 30s) |
| max_missed_heartbeats | Maximum missed heartbeats before marking as dead (default: 3) |
Definition at line 16 of file health_monitor.cpp.
References health_, kcenon::network::utils::connection_health::last_heartbeat, and NETWORK_LOG_INFO.
|
noexcept |
Destructor - stops monitoring if active.
Definition at line 27 of file health_monitor.cpp.
References stop_monitoring().

|
private |
Performs a single heartbeat check.
Sends a heartbeat message and waits for response. Updates health metrics based on the result.
Definition at line 156 of file health_monitor.cpp.
References NETWORK_LOG_DEBUG, and NETWORK_LOG_WARN.
|
nodiscard |
Gets current health status.
Definition at line 139 of file health_monitor.cpp.
References health_, and health_mutex_.
|
nodiscardnoexcept |
Checks if monitoring is active.
Definition at line 151 of file health_monitor.cpp.
References is_monitoring_.
|
private |
Schedules the next heartbeat.
Definition at line 216 of file health_monitor.cpp.
| auto kcenon::network::utils::health_monitor::set_health_callback | ( | std::function< void(const connection_health &)> | callback | ) | -> void |
Sets callback for health status changes.
| callback | Function called when health status changes |
The callback receives the current health metrics.
Definition at line 145 of file health_monitor.cpp.
| auto kcenon::network::utils::health_monitor::start_monitoring | ( | std::shared_ptr< core::messaging_client > | client | ) | -> void |
Starts monitoring the given client.
| client | Client to monitor |
Begins periodic heartbeat checks. If client doesn't respond within the timeout, increments missed_heartbeats counter.
Definition at line 39 of file health_monitor.cpp.
References kcenon::network::integration::thread_integration_manager::instance(), NETWORK_LOG_ERROR, NETWORK_LOG_INFO, NETWORK_LOG_WARN, and kcenon::network::integration::thread_integration_manager::submit_task().

| auto kcenon::network::utils::health_monitor::stop_monitoring | ( | ) | -> void |
Stops monitoring.
Cancels the heartbeat timer and stops the monitoring thread.
Definition at line 100 of file health_monitor.cpp.
References NETWORK_LOG_INFO.
Referenced by ~health_monitor().

|
private |
Updates health metrics.
| success | Whether heartbeat was successful |
| response_time | Response time in milliseconds |
Definition at line 237 of file health_monitor.cpp.
References kcenon::network::integration::monitoring_integration_manager::instance(), and NETWORK_LOG_ERROR.

|
private |
Monitored client
Definition at line 150 of file health_monitor.h.
|
private |
|
private |
Current health metrics
Definition at line 162 of file health_monitor.h.
Referenced by get_health(), and health_monitor().
|
private |
Health callback
Definition at line 168 of file health_monitor.h.
|
mutableprivate |
Protects health metrics
Definition at line 161 of file health_monitor.h.
Referenced by get_health().
|
private |
Heartbeat interval
Definition at line 158 of file health_monitor.h.
|
private |
Heartbeat timer
Definition at line 155 of file health_monitor.h.
|
private |
IO context for timer
Definition at line 152 of file health_monitor.h.
|
private |
Future for io_context execution
Definition at line 156 of file health_monitor.h.
|
private |
Monitoring active flag
Definition at line 164 of file health_monitor.h.
Referenced by is_monitoring().
|
private |
Max missed heartbeats before dead
Definition at line 159 of file health_monitor.h.
|
private |
|
private |
Work guard to keep io_context running
Definition at line 154 of file health_monitor.h.