feat(Logging): per-subsystem token-bucket rate limit on MQTT publish

Protects the broker/airtime from a chatty or misconfigured subsystem (e.g. a
per-subsystem override left at VERBOSE via log.set_mqtt). Each tag gets a
burst of 10 messages, refilling 1/second; once exhausted, further messages
from that tag are dropped until it refills.

Drops are counted, not silently discarded: the next time a rate-limited tag
publishes again, a WARNING summary line reports how many were suppressed.
Serial and SD channels are unaffected.

F-047 in the feature catalog.
This commit is contained in:
2026-07-15 00:04:31 +03:00
parent 2b4fd7c5b0
commit 85cdc55b78
2 changed files with 71 additions and 0 deletions
+54
View File
@@ -16,6 +16,8 @@ Logging::MqttPublishCallback Logging::_mqttCallback = nullptr;
Logging::SdWriteCallback Logging::_sdCallback = nullptr;
String Logging::_mqttLogTopic = "";
std::map<String, Logging::MqttRateState> 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;
+17
View File
@@ -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<String, LogLevel>& 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<String, MqttRateState> _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);
};
// ═══════════════════════════════════════════════════════════════════════════════════