fix: latency calculation
This commit is contained in:
@@ -174,10 +174,10 @@ void ClusterManager::onNodeUpdate(const char* msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (respondingNode) {
|
if (respondingNode) {
|
||||||
// Calculate latency if we recently sent a heartbeat
|
// Calculate latency only if we recently sent a heartbeat (within last 1 second)
|
||||||
unsigned long latency = 0;
|
unsigned long latency = 0;
|
||||||
if (lastHeartbeatSentAt != 0) {
|
unsigned long now = millis();
|
||||||
unsigned long now = millis();
|
if (lastHeartbeatSentAt != 0 && (now - lastHeartbeatSentAt) < 1000) { // 1 second window
|
||||||
latency = now - lastHeartbeatSentAt;
|
latency = now - lastHeartbeatSentAt;
|
||||||
lastHeartbeatSentAt = 0; // Reset for next calculation
|
lastHeartbeatSentAt = 0; // Reset for next calculation
|
||||||
}
|
}
|
||||||
@@ -201,11 +201,15 @@ void ClusterManager::onNodeUpdate(const char* msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
respondingNode->lastSeen = millis();
|
respondingNode->lastSeen = now;
|
||||||
respondingNode->status = NodeInfo::ACTIVE;
|
respondingNode->status = NodeInfo::ACTIVE;
|
||||||
respondingNode->latency = latency;
|
|
||||||
|
|
||||||
LOG_DEBUG("Cluster", String("Updated responding node ") + respondingNode->hostname + " @ " + respondingNodeIP.toString() + " | latency: " + String(latency) + "ms");
|
// Update latency if we calculated it (preserve existing value if not)
|
||||||
|
if (latency > 0) {
|
||||||
|
respondingNode->latency = latency;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_DEBUG("Cluster", String("Updated responding node ") + respondingNode->hostname + " @ " + respondingNodeIP.toString() + " | latency: " + String(latency > 0 ? String(latency) + "ms" : "not calculated"));
|
||||||
} else {
|
} else {
|
||||||
LOG_WARN("Cluster", String("Received NODE_UPDATE from unknown node: ") + respondingNodeIP.toString());
|
LOG_WARN("Cluster", String("Received NODE_UPDATE from unknown node: ") + respondingNodeIP.toString());
|
||||||
}
|
}
|
||||||
@@ -318,9 +322,10 @@ void ClusterManager::addOrUpdateNode(const String& nodeHost, IPAddress nodeIP) {
|
|||||||
// O(1) lookup instead of O(n) search
|
// O(1) lookup instead of O(n) search
|
||||||
auto it = memberList.find(nodeHost);
|
auto it = memberList.find(nodeHost);
|
||||||
if (it != memberList.end()) {
|
if (it != memberList.end()) {
|
||||||
// Update existing node
|
// Update existing node - preserve all existing field values
|
||||||
it->second.ip = nodeIP;
|
it->second.ip = nodeIP;
|
||||||
it->second.lastSeen = millis();
|
it->second.lastSeen = millis();
|
||||||
|
// Note: Other fields like latency, uptime, labels, etc. are preserved
|
||||||
//fetchNodeInfo(nodeIP); // Do not fetch here, handled by periodic task
|
//fetchNodeInfo(nodeIP); // Do not fetch here, handled by periodic task
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user