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
+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);
};
// ═══════════════════════════════════════════════════════════════════════════════════