Skip to content

Commit

Permalink
catch bad_alloc for Huawei and Pylontech WebApi_ws
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Apr 5, 2023
1 parent 0c34554 commit c0dff1e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 41 deletions.
50 changes: 29 additions & 21 deletions src/WebApi_ws_Huawei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ void WebApiWsHuaweiLiveClass::init(AsyncWebServer* server)

void WebApiWsHuaweiLiveClass::loop()
{


// see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients
if (millis() - _lastWsCleanup > 1000) {
_ws.cleanupClients();
Expand All @@ -51,24 +49,28 @@ void WebApiWsHuaweiLiveClass::loop()
}
_lastUpdateCheck = millis();

DynamicJsonDocument root(1024);
JsonVariant var = root;
generateJsonResponse(var);
try {
String buffer;
// free JsonDocument as soon as possible
{
DynamicJsonDocument root(1024);
JsonVariant var = root;
generateJsonResponse(var);
serializeJson(root, buffer);
}

String buffer;
if (buffer) {
serializeJson(root, buffer);
if (buffer) {
if (Configuration.get().Security_AllowReadonly) {
_ws.setAuthentication("", "");
} else {
_ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password);
}

if (Configuration.get().Security_AllowReadonly) {
_ws.setAuthentication("", "");
} else {
_ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password);
_ws.textAll(buffer);
}

_ws.textAll(buffer);
} catch (std::bad_alloc& bad_alloc) {
MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what());
}


}

void WebApiWsHuaweiLiveClass::generateJsonResponse(JsonVariant& root)
Expand Down Expand Up @@ -119,10 +121,16 @@ void WebApiWsHuaweiLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
if (!WebApi.checkCredentialsReadonly(request)) {
return;
}
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
JsonVariant root = response->getRoot().as<JsonVariant>();
generateJsonResponse(root);
try {
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
JsonVariant root = response->getRoot().as<JsonVariant>();
generateJsonResponse(root);

response->setLength();
request->send(response);
} catch (std::bad_alloc& bad_alloc) {
MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what());

response->setLength();
request->send(response);
WebApi.sendTooManyRequests(request);
}
}
50 changes: 30 additions & 20 deletions src/WebApi_ws_Pylontech.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,28 @@ void WebApiWsPylontechLiveClass::loop()
}
_lastUpdateCheck = millis();

DynamicJsonDocument root(1024);
JsonVariant var = root;
generateJsonResponse(var);

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

if (Configuration.get().Security_AllowReadonly) {
_ws.setAuthentication("", "");
} else {
_ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password);
try {
String buffer;
// free JsonDocument as soon as possible
{
DynamicJsonDocument root(1024);
JsonVariant var = root;
generateJsonResponse(var);
serializeJson(root, buffer);
}

_ws.textAll(buffer);
}
if (buffer) {
if (Configuration.get().Security_AllowReadonly) {
_ws.setAuthentication("", "");
} else {
_ws.setAuthentication(AUTH_USERNAME, Configuration.get().Security_Password);
}


_ws.textAll(buffer);
}
} catch (std::bad_alloc& bad_alloc) {
MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what());
}
}

void WebApiWsPylontechLiveClass::generateJsonResponse(JsonVariant& root)
Expand Down Expand Up @@ -137,10 +141,16 @@ void WebApiWsPylontechLiveClass::onLivedataStatus(AsyncWebServerRequest* request
if (!WebApi.checkCredentialsReadonly(request)) {
return;
}
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
JsonVariant root = response->getRoot().as<JsonVariant>();
generateJsonResponse(root);
try {
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
JsonVariant root = response->getRoot().as<JsonVariant>();
generateJsonResponse(root);

response->setLength();
request->send(response);
response->setLength();
request->send(response);
} catch (std::bad_alloc& bad_alloc) {
MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what());

WebApi.sendTooManyRequests(request);
}
}

0 comments on commit c0dff1e

Please sign in to comment.