diff --git a/src/Logging/Logging.cpp b/src/Logging/Logging.cpp index 14cbced..af39c5e 100644 --- a/src/Logging/Logging.cpp +++ b/src/Logging/Logging.cpp @@ -16,6 +16,8 @@ Logging::MqttPublishCallback Logging::_mqttCallback = nullptr; Logging::SdWriteCallback Logging::_sdCallback = nullptr; String Logging::_mqttLogTopic = ""; +std::map Logging::_mqttRateStates; + // ═══════════════════════════════════════════════════════════════════════════════════ // GLOBAL CHANNEL LEVEL SETTERS / GETTERS // ═══════════════════════════════════════════════════════════════════════════════════ @@ -184,6 +186,57 @@ void Logging::log(LogLevel level, const char* levelStr, const char* tag, const c } } +// ═══════════════════════════════════════════════════════════════════════════════════ +// PRIVATE: MQTT RATE LIMIT +// Per-subsystem token bucket. Returns false (and counts a drop) once a tag's +// burst allowance is exhausted; refills at a fixed rate over time. +// ═══════════════════════════════════════════════════════════════════════════════════ + +bool Logging::allowMqttPublish(const char* tag) { + MqttRateState& state = _mqttRateStates[String(tag)]; + uint32_t now = millis(); + + // First time seeing this tag — start full. + if (state.lastRefillMs == 0) { + state.tokens = MQTT_RATE_BURST; + state.lastRefillMs = now; + } + + uint32_t elapsed = now - state.lastRefillMs; + if (elapsed >= MQTT_RATE_REFILL_MS) { + uint32_t refill = elapsed / MQTT_RATE_REFILL_MS; + uint32_t newTokens = (uint32_t)state.tokens + refill; + state.tokens = (uint8_t)(newTokens > MQTT_RATE_BURST ? MQTT_RATE_BURST : newTokens); + state.lastRefillMs = now; + } + + if (state.tokens == 0) { + state.droppedSinceLastPublish++; + return false; + } + + // If we previously dropped messages for this tag, surface a summary now + // that the bucket has room again — visibility without the flood. + if (state.droppedSinceLastPublish > 0 && _mqttCallback && !_mqttLogTopic.isEmpty()) { + String payload; + payload.reserve(160); + payload = "{\"level\":\"WARNING\",\"subsystem\":\""; + payload += tag; + payload += "\",\"message\":\"suppressed "; + payload += state.droppedSinceLastPublish; + payload += " log message(s) (MQTT rate limit)\",\"timestamp\":"; + payload += now; + payload += "}"; + _mqttCallback(_mqttLogTopic, payload, 1); + state.droppedSinceLastPublish = 0; + state.tokens--; // the summary line itself costs a token + return state.tokens > 0; + } + + state.tokens--; + return true; +} + // ═══════════════════════════════════════════════════════════════════════════════════ // PRIVATE: MQTT PUBLISH // Uses a re-entrancy guard to prevent log-of-a-log recursion. @@ -191,6 +244,7 @@ void Logging::log(LogLevel level, const char* levelStr, const char* tag, const c void Logging::publishToMqtt(LogLevel level, const char* levelStr, const char* tag, const char* message) { if (!_mqttCallback || _mqttLogTopic.isEmpty()) return; + if (!allowMqttPublish(tag)) return; static bool isPublishing = false; if (isPublishing) return; diff --git a/src/Logging/Logging.hpp b/src/Logging/Logging.hpp index 7ac402b..4993313 100644 --- a/src/Logging/Logging.hpp +++ b/src/Logging/Logging.hpp @@ -129,6 +129,23 @@ private: // Resolve effective level for a tag on a channel static LogLevel resolveLevel(const char* tag, LogLevel globalLevel, const std::map& overrides); + + // ═══════════════════════════════════════════════════════════════════════════════ + // MQTT RATE LIMITING + // Per-subsystem token bucket. Protects the broker/airtime from a chatty or + // misconfigured subsystem (e.g. a per-subsystem override left at VERBOSE). + // Drops are counted, not silently discarded — a summary line is emitted the + // next time that subsystem is allowed to publish again. + // ═══════════════════════════════════════════════════════════════════════════════ + struct MqttRateState { + uint8_t tokens; + uint32_t lastRefillMs; + uint32_t droppedSinceLastPublish; + }; + static std::map _mqttRateStates; + static constexpr uint8_t MQTT_RATE_BURST = 10; // max messages in a burst + static constexpr uint32_t MQTT_RATE_REFILL_MS = 1000; // 1 token regained per second + static bool allowMqttPublish(const char* tag); }; // ═══════════════════════════════════════════════════════════════════════════════════