Reserve SDK client startup memory before MQTT TLS admission
This commit is contained in:
1 parent
94cc96f234
commit
beb5b69a80
9 files changed
+162
-25
No files matched your search
@@ -11,12 +11,24 @@ namespace MQTTConnectionAdmission {
|
||||
|
||||
static const size_t kDmaReserveBytes = 16384;
|
||||
static const size_t kDmaLargestBytes = 4096;
|
||||
// A stopped SDK client needs a new internal task stack/TCB. First starts also
|
||||
// allocate receive/transmit/reassembly buffers and SDK transport/config objects
|
||||
// after admission. Budget that ordinary allocation separately from AES headroom.
|
||||
// Pin the actual SDK stack/priority in the optimizer rather than relying on an
|
||||
// SDK default or confusing this per-client task with the MQTTBridge worker.
|
||||
static const size_t kClientTaskStackBytes = 6144;
|
||||
static const int kClientTaskPriority = 5;
|
||||
static const size_t kColdStartAllowanceBytes = 16384;
|
||||
static const size_t kColdStartLargestBytes = 8192;
|
||||
static const size_t kTlsInternalFreeBytes = 61440;
|
||||
static const size_t kTlsRecordAllocationBytes = 17408;
|
||||
|
||||
static inline bool canStart(bool psram, bool tls, size_t dma_free,
|
||||
size_t dma_largest) {
|
||||
if (dma_free < kDmaReserveBytes || dma_largest < kDmaLargestBytes) {
|
||||
static inline bool canStart(bool psram, bool tls, bool cold_start,
|
||||
size_t dma_free, size_t dma_largest) {
|
||||
const size_t free_required = kDmaReserveBytes +
|
||||
(cold_start ? kColdStartAllowanceBytes : 0);
|
||||
const size_t largest_required = cold_start ? kColdStartLargestBytes : kDmaLargestBytes;
|
||||
if (dma_free < free_required || dma_largest < largest_required) {
|
||||
return false;
|
||||
}
|
||||
return !tls || psram ||
|
||||
|
||||
@@ -2088,9 +2088,14 @@ bool MQTTBridge::canStartSlotConnection(int index) const {
|
||||
const char* uri = slot.preset ? slot.preset->server_url
|
||||
: (slot.broker_uri[0] ? slot.broker_uri : slot.host);
|
||||
const bool tls = MQTTConnectionAdmission::requiresTls(uri, slot.port);
|
||||
// A never-created or stopped SDK client still allocates a task and ordinary
|
||||
// SDK buffers after this check. Retained handles with a stopped task are
|
||||
// conservatively charged the same allowance as first starts. A started
|
||||
// reconnect keeps that task/buffer memory and needs only handshake headroom.
|
||||
const bool cold_start = slot.client == nullptr || !slot.client->isStarted();
|
||||
const uint32_t caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA;
|
||||
return MQTTConnectionAdmission::canStart(
|
||||
ESP.getPsramSize() > 0, tls, heap_caps_get_free_size(caps),
|
||||
ESP.getPsramSize() > 0, tls, cold_start, heap_caps_get_free_size(caps),
|
||||
heap_caps_get_largest_free_block(caps));
|
||||
#else
|
||||
return true;
|
||||
@@ -5079,6 +5084,12 @@ void MQTTBridge::getClientVersion(char* buffer, size_t buffer_size) const {
|
||||
void MQTTBridge::optimizeMqttClientConfig(PsychicMqttClient* client, bool needs_large_buffer) {
|
||||
if (!client) return;
|
||||
|
||||
// Admission reserves this SDK client's internal stack and startup allocations
|
||||
// separately from the long-lived MQTTBridge worker stack. Keep IDF4 and IDF5
|
||||
// on the same explicit settings so a changed SDK default cannot bypass it.
|
||||
client->setTaskStackAndPriority(MQTTConnectionAdmission::kClientTaskStackBytes,
|
||||
MQTTConnectionAdmission::kClientTaskPriority);
|
||||
|
||||
// Cloudflare closes WebSocket connections after 100s idle (non-configurable).
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
client->setKeepAlive(45);
|
||||
|
||||
Reference in new issue
Block a user