17 size_t max_missed_heartbeats)
18 : heartbeat_interval_(heartbeat_interval)
19 , max_missed_heartbeats_(max_missed_heartbeats)
23 std::to_string(heartbeat_interval.count()) +
"s, max_missed=" +
24 std::to_string(max_missed_heartbeats));
40 std::shared_ptr<core::messaging_client> client) ->
void
42 if (is_monitoring_.load())
57 io_context_ = std::make_unique<asio::io_context>();
58 work_guard_ = std::make_unique<asio::executor_work_guard<asio::io_context::executor_type>>(
59 asio::make_work_guard(*io_context_)
63 heartbeat_timer_ = std::make_unique<asio::steady_timer>(*io_context_);
65 is_monitoring_.store(
true);
68 total_heartbeats_.store(0);
69 failed_heartbeats_.store(0);
71 std::lock_guard<std::mutex> lock(health_mutex_);
72 health_.is_alive =
true;
73 health_.missed_heartbeats = 0;
74 health_.packet_loss_rate = 0.0;
75 health_.last_heartbeat = std::chrono::steady_clock::now();
86 catch (
const std::exception& e)
89 std::string(e.what()));
95 schedule_next_heartbeat();
102 if (!is_monitoring_.exchange(
false))
108 if (heartbeat_timer_)
110 heartbeat_timer_->cancel();
126 if (io_future_.valid())
132 heartbeat_timer_.reset();
148 health_callback_ = std::move(callback);
158 if (!is_monitoring_.load() || !client_)
163 auto start_time = std::chrono::steady_clock::now();
166 bool is_connected = client_->is_connected();
168 total_heartbeats_.fetch_add(1);
173 std::vector<uint8_t> heartbeat_data = {
179 auto send_result = client_->send_packet(std::move(heartbeat_data));
181 auto end_time = std::chrono::steady_clock::now();
182 auto response_time = std::chrono::duration_cast<std::chrono::milliseconds>(
183 end_time - start_time);
185 if (!send_result.is_err())
188 update_health(
true, response_time);
190 std::to_string(response_time.count()) +
"ms");
195 failed_heartbeats_.fetch_add(1);
196 update_health(
false, response_time);
198 send_result.error().message);
204 failed_heartbeats_.fetch_add(1);
205 update_health(
false, std::chrono::milliseconds(0));
210 if (is_monitoring_.load())
212 schedule_next_heartbeat();
218 if (!heartbeat_timer_ || !is_monitoring_.load())
223 heartbeat_timer_->expires_after(heartbeat_interval_);
225 auto self = shared_from_this();
226 heartbeat_timer_->async_wait(
227 [
this, self](
const std::error_code& ec)
229 if (!ec && is_monitoring_.load())
238 std::chrono::milliseconds response_time) ->
void
240 std::lock_guard<std::mutex> lock(health_mutex_);
242 health_.last_heartbeat = std::chrono::steady_clock::now();
243 health_.last_response_time = response_time;
248 health_.missed_heartbeats = 0;
249 health_.is_alive =
true;
254 health_.missed_heartbeats++;
257 if (health_.missed_heartbeats >= max_missed_heartbeats_)
259 health_.is_alive =
false;
261 std::to_string(health_.missed_heartbeats) +
" heartbeats)");
266 auto total = total_heartbeats_.load();
267 auto failed = failed_heartbeats_.load();
270 health_.packet_loss_rate =
static_cast<double>(failed) /
static_cast<double>(total);
277 auto monitor = monitor_mgr.get_monitoring();
278 if (monitor && client_)
281 std::ostringstream oss;
282 oss <<
"client_" <<
static_cast<const void*
>(client_.get());
284 monitor->report_health(
287 static_cast<double>(health_.last_response_time.count()),
288 health_.missed_heartbeats,
289 health_.packet_loss_rate
299 if (health_callback_)
301 health_callback_(health_);
static monitoring_integration_manager & instance()
Get the singleton instance.
static thread_integration_manager & instance()
Get the singleton instance.
std::future< void > submit_task(std::function< void()> task)
Submit a task to the thread pool.
auto set_health_callback(std::function< void(const connection_health &)> callback) -> void
Sets callback for health status changes.
std::atomic< bool > is_monitoring_
auto schedule_next_heartbeat() -> void
Schedules the next heartbeat.
auto do_heartbeat() -> void
Performs a single heartbeat check.
auto start_monitoring(std::shared_ptr< core::messaging_client > client) -> void
Starts monitoring the given client.
auto update_health(bool success, std::chrono::milliseconds response_time) -> void
Updates health metrics.
~health_monitor() noexcept
Destructor - stops monitoring if active.
connection_health health_
auto get_health() const -> connection_health
Gets current health status.
auto stop_monitoring() -> void
Stops monitoring.
auto is_monitoring() const noexcept -> bool
Checks if monitoring is active.
health_monitor(std::chrono::seconds heartbeat_interval=std::chrono::seconds(30), size_t max_missed_heartbeats=3)
Constructs a health monitor.
Logger system integration interface for network_system.
#define NETWORK_LOG_WARN(msg)
#define NETWORK_LOG_INFO(msg)
#define NETWORK_LOG_ERROR(msg)
#define NETWORK_LOG_DEBUG(msg)
Monitoring system integration interface for network_system.
Utility components for network_system.
Contains connection health metrics.
std::chrono::steady_clock::time_point last_heartbeat
Thread system integration interface for network_system.