Skip to content

Commit

Permalink
Better handling of out of memory situations in live data websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody authored and helgeerbe committed Jan 5, 2024
1 parent f00cd1b commit f968179
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/WebApi_ws_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Configuration.h"
#include "Datastore.h"
#include "MessageOutput.h"
#include "Utils.h"
#include "WebApi.h"
#include "Battery.h"
#include "Huawei_can.h"
Expand Down Expand Up @@ -67,20 +68,12 @@ void WebApiWsLiveClass::loop()

try {
std::lock_guard<std::mutex> lock(_mutex);
DynamicJsonDocument root(4200 * INV_MAX_COUNT); // TODO(helge) check if this calculation is correct
DynamicJsonDocument root(4096 * INV_MAX_COUNT);
if (Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
JsonVariant var = root;
generateJsonResponse(var);

// TODO(helge) temporary dump of memory usage if allocation of DynamicJsonDocument fails (will be fixed in upstream repo)
if (root.capacity() == 0) {
MessageOutput.printf("Calling /api/livedata/status has temporarily run out of resources. Reason: Alloc memory for DynamicJsonDocument failed (FreeHeap = %d, MaxAllocHeap = %d, MinFreeHeap = %d).\r\n", ESP.getFreeHeap(), ESP.getMaxAllocHeap(), ESP.getMinFreeHeap());
_lastWsPublish = millis();
return;
}

JsonVariant var = root;
generateJsonResponse(var);

String buffer;
if (buffer) {
String buffer;
serializeJson(root, buffer);

if (Configuration.get().Security.AllowReadonly) {
Expand Down

0 comments on commit f968179

Please sign in to comment.