Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
health_monitor.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <atomic>
8#include <chrono>
9#include <functional>
10#include <future>
11#include <memory>
12#include <mutex>
13
14#include <asio.hpp>
15
17
19{
20
26 {
27 bool is_alive{true};
28 std::chrono::milliseconds last_response_time{0};
30 double packet_loss_rate{0.0};
31 std::chrono::steady_clock::time_point last_heartbeat;
32 };
33
74 class health_monitor : public std::enable_shared_from_this<health_monitor>
75 {
76 public:
82 explicit health_monitor(
83 std::chrono::seconds heartbeat_interval = std::chrono::seconds(30),
84 size_t max_missed_heartbeats = 3);
85
89 ~health_monitor() noexcept;
90
98 auto start_monitoring(std::shared_ptr<core::messaging_client> client) -> void;
99
105 auto stop_monitoring() -> void;
106
111 [[nodiscard]] auto get_health() const -> connection_health;
112
120 std::function<void(const connection_health&)> callback) -> void;
121
126 [[nodiscard]] auto is_monitoring() const noexcept -> bool;
127
128 private:
135 auto do_heartbeat() -> void;
136
140 auto schedule_next_heartbeat() -> void;
141
147 auto update_health(bool success, std::chrono::milliseconds response_time) -> void;
148
149 private:
150 std::shared_ptr<core::messaging_client> client_;
152 std::unique_ptr<asio::io_context> io_context_;
153 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
155 std::unique_ptr<asio::steady_timer> heartbeat_timer_;
156 std::future<void> io_future_;
158 std::chrono::seconds heartbeat_interval_;
161 mutable std::mutex health_mutex_;
164 std::atomic<bool> is_monitoring_{false};
165 std::atomic<size_t> total_heartbeats_{0};
166 std::atomic<size_t> failed_heartbeats_{0};
168 std::function<void(const connection_health&)> health_callback_;
169 };
170
171} // namespace kcenon::network::utils
Monitors connection health with heartbeat mechanism.
auto set_health_callback(std::function< void(const connection_health &)> callback) -> void
Sets callback for health status changes.
std::unique_ptr< asio::steady_timer > heartbeat_timer_
auto schedule_next_heartbeat() -> void
Schedules the next heartbeat.
auto do_heartbeat() -> void
Performs a single heartbeat check.
std::shared_ptr< core::messaging_client > client_
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.
std::function< void(const connection_health &)> health_callback_
~health_monitor() noexcept
Destructor - stops monitoring if active.
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.
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
std::unique_ptr< asio::io_context > io_context_
health_monitor(std::chrono::seconds heartbeat_interval=std::chrono::seconds(30), size_t max_missed_heartbeats=3)
Constructs a health monitor.
TCP client implementation.
Utility components for network_system.
Contains connection health metrics.
std::chrono::steady_clock::time_point last_heartbeat
std::chrono::milliseconds last_response_time