health_monitor.cpp¶
src/mcp/health_monitor.cpp
@file health_monitor.cpp @brief HealthMonitor implementation.
Owners: @tvanfossen
attempt_reconnect
void entropic::HealthMonitor::attempt_reconnect(const std::string &name, WatchEntry &entry)
Attempt reconnection for a single server.
src/mcp/health_monitor.cpp:219
Called by check_server
Calls connect, delay_ms, exhausted, on_reconnect_success, post_event
Source · 40 lines
void HealthMonitor::attempt_reconnect(
const std::string& name,
WatchEntry& entry) {
if (policy_.exhausted(entry.reconnect_attempt)) {
if (entry.status != "error") {
auto old = entry.status;
entry.status = "error";
post_event(name, old, "error");
logger->error("Server '{}' reconnection exhausted "
"after {} attempts",
name, entry.reconnect_attempt);
}
entry.next_action = std::chrono::steady_clock::time_point::max();
return;
}
if (entry.status != "reconnecting") {
auto old = entry.status;
entry.status = "reconnecting";
post_event(name, old, "reconnecting");
}
logger->info("Reconnecting to '{}' (attempt {})",
name, entry.reconnect_attempt + 1);
if (entry.client->connect()) {
on_reconnect_success(name, entry);
return;
}
// Schedule next attempt with backoff
auto delay = policy_.delay_ms(entry.reconnect_attempt);
entry.reconnect_attempt++;
entry.next_action = std::chrono::steady_clock::now() +
std::chrono::milliseconds(delay);
logger->info("Server '{}' reconnect failed, "
"retry in {}ms", name, delay);
}Activity · 20 steps
Requirements
Flows
check_server
void entropic::HealthMonitor::check_server(const std::string &name, WatchEntry &entry)
Check one server and handle state transitions.
src/mcp/health_monitor.cpp:174
Source · 32 lines
void HealthMonitor::check_server(
const std::string& name,
WatchEntry& entry) {
bool alive = entry.client->is_connected();
if (entry.status == "connected" && !alive) {
// Connection lost — start reconnecting
auto old = entry.status;
entry.status = "disconnected";
entry.reconnect_attempt = 0;
post_event(name, old, entry.status);
logger->warn("Server '{}' disconnected", name);
attempt_reconnect(name, entry);
return;
}
if (entry.status == "disconnected" ||
entry.status == "reconnecting") {
attempt_reconnect(name, entry);
return;
}
// Connected + healthy: schedule next check
if (health_check_interval_ms_ > 0) {
entry.next_action = std::chrono::steady_clock::now() +
std::chrono::milliseconds(health_check_interval_ms_);
} else {
entry.next_action = std::chrono::steady_clock::now() +
std::chrono::seconds(5);
}
}Activity · 16 steps
Requirements
Flows
monitor_loop
void entropic::HealthMonitor::monitor_loop()
Monitor loop (background thread).
src/mcp/health_monitor.cpp:143
Source · 19 lines
void HealthMonitor::monitor_loop() {
constexpr auto poll_interval = std::chrono::milliseconds(500);
while (running_) {
{
std::lock_guard<std::mutex> lock(watched_mutex_);
auto now = std::chrono::steady_clock::now();
for (auto& [name, entry] : watched_) {
if (now >= entry.next_action) {
check_server(name, entry);
}
}
}
std::unique_lock<std::mutex> lock(wake_mutex_);
wake_cv_.wait_for(lock, poll_interval,
[this] { return !running_.load(); });
}
}Activity · 9 steps
Flows
on_reconnect_success
void entropic::HealthMonitor::on_reconnect_success(const std::string &name, WatchEntry &entry)
Handle a successful reconnect: refresh tools, mark connected, enqueue the event.
src/mcp/health_monitor.cpp:273
Source · 20 lines
void HealthMonitor::on_reconnect_success(const std::string& name,
WatchEntry& entry) {
auto [added, removed] = entry.client->refresh_tools();
entry.status = "connected";
entry.reconnect_attempt = 0;
HealthEvent evt;
evt.server_name = name;
evt.old_status = "reconnecting";
evt.new_status = "connected";
evt.added_tools = std::move(added);
evt.removed_tools = std::move(removed);
{
std::lock_guard<std::mutex> lock(event_mutex_);
event_queue_.push_back(std::move(evt));
}
logger->info("Server '{}' reconnected", name);
entry.next_action = std::chrono::steady_clock::now();
}Activity · 13 steps
Requirements
post_event
void entropic::HealthMonitor::post_event(const std::string &name, const std::string &old_status, const std::string &new_status)
Post a status change event to the queue.
src/mcp/health_monitor.cpp:306
Source · 14 lines
void HealthMonitor::post_event(
const std::string& name,
const std::string& old_status,
const std::string& new_status) {
HealthEvent evt;
evt.server_name = name;
evt.old_status = old_status;
evt.new_status = new_status;
{
std::lock_guard<std::mutex> lock(event_mutex_);
event_queue_.push_back(std::move(evt));
}
}Activity · 6 steps
Requirements
Flows
process_events
void entropic::HealthMonitor::process_events()
Drain event queue, invoke callbacks (call on engine thread).
src/mcp/health_monitor.cpp:124
Source · 13 lines
void HealthMonitor::process_events() {
std::vector<HealthEvent> events;
{
std::lock_guard<std::mutex> lock(event_mutex_);
events.swap(event_queue_);
}
for (const auto& event : events) {
if (status_callback_) {
status_callback_(event);
}
}
}Activity · 6 steps
Requirements
start
void entropic::HealthMonitor::start()
Start the monitoring thread.
src/mcp/health_monitor.cpp:87
Source · 9 lines
void HealthMonitor::start() {
if (running_) {
return;
}
running_ = true;
monitor_thread_ = std::thread(
&HealthMonitor::monitor_loop, this);
logger->info("Health monitor started");
}Activity · 5 steps
Requirements
watch
void entropic::HealthMonitor::watch(const std::string &name, ExternalMCPClient *client)
Start monitoring a server.
src/mcp/health_monitor.cpp:44
Source · 15 lines
void HealthMonitor::watch(
const std::string& name,
ExternalMCPClient* client) {
std::lock_guard<std::mutex> lock(watched_mutex_);
WatchEntry entry;
entry.client = client;
entry.status = client->is_connected() ? "connected" : "disconnected";
entry.reconnect_attempt = 0;
entry.next_action = std::chrono::steady_clock::now();
watched_[name] = entry;
logger->info("Watching server '{}'", name);
wake_cv_.notify_one();
}