feat(mqtt): count connect failures and report tripped breakers
Two gaps remained after the first connection-health commit. A slot retrying into a heap that cannot satisfy its TLS allocation moved no counter at all. disconnect_count rose, but it also rises for dropped live sessions, so it could not separate "actively failing to connect" from "was connected, dropped once". Adds a per-slot connect_failures, incremented in onDisconnect when the event arrives while the client is Starting -- meaning it ended an attempt that never reached onConnect. Late events from a stopped or quarantined client, and soft-disconnects of a live session, are in other states and are not miscounted. A slot parked by the circuit breaker also looked identical to one still retrying, despite being materially worse: the breaker only probes every 30 minutes, so recovery is far slower and an operator may need to intervene. Surfaces both: - `mqttN.diag` gains `cf:<n>` beside the existing `dc:<n>`. - the status payload gains `mqtt_connect_failures` (summed across slots) and `mqtt_slots_breaker` (count of tripped slots), each emitted only when non-zero so a healthy board's payload does not grow. The counter is what distinguishes a retrying slot from a quiet one: while a slot is down its publish counters cannot move by definition, so this is the only value that shows the device is still trying. Both suggested by adversarial review.
This commit is contained in:
1 parent
383339eccc
commit
7004b81c0c
5 files changed
+81
-1
No files matched your search
@@ -118,6 +118,46 @@ TEST(MQTTPayloadBuilder, StatusReportsConnectionHealthWhenSupplied) {
|
||||
EXPECT_EQ(16116, stats["heap_largest"].as<int>());
|
||||
}
|
||||
|
||||
TEST(MQTTPayloadBuilder, StatusReportsConnectFailuresAndBreakerOnlyWhenNonZero) {
|
||||
JsonDocument scratch;
|
||||
char buffer[1024];
|
||||
MQTTConnHealth health;
|
||||
health.slots_up = 1;
|
||||
health.slots_total = 3;
|
||||
health.connect_failures = 57;
|
||||
health.slots_breaker = 1;
|
||||
|
||||
int len = MQTTPayloadBuilder::buildStatusMessage(
|
||||
scratch, "node", "id", "model", "firmware", "radio", "client", "online",
|
||||
kTimestamp, buffer, sizeof(buffer), -1, -1, -1, -1, -999,
|
||||
-1, -1, -1, -1, -1, -1, nullptr, health);
|
||||
|
||||
ASSERT_GT(len, 0);
|
||||
JsonDocument parsed;
|
||||
ASSERT_FALSE(deserializeJson(parsed, buffer));
|
||||
JsonObject stats = parsed["stats"].as<JsonObject>();
|
||||
EXPECT_EQ(57, stats["mqtt_connect_failures"].as<int>());
|
||||
EXPECT_EQ(1, stats["mqtt_slots_breaker"].as<int>());
|
||||
|
||||
// A healthy board must not carry either key: zero connect failures and zero
|
||||
// tripped breakers are the normal case, and the steady-state payload is
|
||||
// already close to the buffer bound.
|
||||
MQTTConnHealth healthy;
|
||||
healthy.slots_up = 3;
|
||||
healthy.slots_total = 3;
|
||||
healthy.connect_failures = 0;
|
||||
healthy.slots_breaker = 0;
|
||||
ASSERT_GT(MQTTPayloadBuilder::buildStatusMessage(
|
||||
scratch, "node", "id", "model", "firmware", "radio", "client", "online",
|
||||
kTimestamp, buffer, sizeof(buffer), -1, -1, -1, -1, -999,
|
||||
-1, -1, -1, -1, -1, -1, nullptr, healthy), 0);
|
||||
JsonDocument healthy_parsed;
|
||||
ASSERT_FALSE(deserializeJson(healthy_parsed, buffer));
|
||||
JsonObject healthy_stats = healthy_parsed["stats"].as<JsonObject>();
|
||||
EXPECT_FALSE(healthy_stats["mqtt_connect_failures"].is<JsonVariant>());
|
||||
EXPECT_FALSE(healthy_stats["mqtt_slots_breaker"].is<JsonVariant>());
|
||||
}
|
||||
|
||||
// The outage key doubles as the "something is down" flag, so its absence has to be
|
||||
// meaningful: a fully healthy board must not emit it.
|
||||
TEST(MQTTPayloadBuilder, StatusOmitsOutageKeyWhenEverySlotIsUp) {
|
||||
@@ -168,6 +208,8 @@ TEST(MQTTPayloadBuilder, WorstCaseStatusWithConnectionHealthFitsProductionBuffer
|
||||
health.slots_total = 5; // PSRAM slot cap, the widest configuration
|
||||
health.worst_outage_secs = 4294967; // millis() rollover / 1000, the widest value
|
||||
health.heap_largest = 8388608;
|
||||
health.connect_failures = 2147483647;
|
||||
health.slots_breaker = 5;
|
||||
|
||||
int len = MQTTPayloadBuilder::buildStatusMessage(
|
||||
scratch, origin.c_str(), origin_id.c_str(), model.c_str(), firmware.c_str(),
|
||||
|
||||
Reference in new issue
Block a user