Skip to content

Commit 9c33b5c

Browse files
committed
limit mqtt queue to 300
1 parent ba7ceca commit 9c33b5c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/mqtt.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,20 @@ bool Mqtt::queue_message(const uint8_t operation, const std::string & topic, con
594594
}
595595
// check free mem
596596
#ifndef EMSESP_STANDALONE
597-
if (ESP.getFreeHeap() < 60 * 1204) {
597+
if (ESP.getFreeHeap() < 60 * 1204 || ESP.getMaxAllocHeap() < 40 * 1024) {
598598
if (operation == Operation::PUBLISH) {
599599
mqtt_message_id_++;
600600
mqtt_publish_fails_++;
601601
}
602-
LOG_DEBUG("%s failed: low memory", operation == Operation::PUBLISH ? "Publish" : operation == Operation::SUBSCRIBE ? "Subscribe" : "Unsubscribe");
602+
LOG_WARNING("%s failed: low memory", operation == Operation::PUBLISH ? "Publish" : operation == Operation::SUBSCRIBE ? "Subscribe" : "Unsubscribe");
603+
return false; // quit
604+
}
605+
if (queuecount_ >= MQTT_QUEUE_MAX_SIZE) {
606+
if (operation == Operation::PUBLISH) {
607+
mqtt_message_id_++;
608+
mqtt_publish_fails_++;
609+
}
610+
LOG_WARNING("%s failed: queue full", operation == Operation::PUBLISH ? "Publish" : operation == Operation::SUBSCRIBE ? "Subscribe" : "Unsubscribe");
603611
return false; // quit
604612
}
605613
#endif

src/mqtt.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class Mqtt {
5555
enum Operation : uint8_t { PUBLISH, SUBSCRIBE, UNSUBSCRIBE };
5656
enum NestedFormat : uint8_t { NESTED = 1, SINGLE };
5757

58-
static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 128; // fixed, not a user setting anymore
58+
static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 128; // fixed, not a user setting anymore
59+
static constexpr uint16_t MQTT_QUEUE_MAX_SIZE = 300;
5960

6061
static void on_connect();
6162
static void on_disconnect(espMqttClientTypes::DisconnectReason reason);

0 commit comments

Comments
 (0)