mirror of
https://github.com/tbnobody/OpenDTU.git
synced 2025-12-11 01:10:20 +01:00
feat(WebApi_prometheus.cpp): dynamically adjust the initial response buffer size
the current implementation allocates a 40kB buffer right from the start. In most cases this is way too big. This patch dynamically learns the right required size for the local setup and preallocates just as much space as needed. Signed-off-by: Peter Lieven <pl@dlhnet.de>
This commit is contained in:
@@ -20,12 +20,14 @@ void WebApiPrometheusClass::init(AsyncWebServer& server, Scheduler& scheduler)
|
||||
|
||||
void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* request)
|
||||
{
|
||||
static size_t initialResponseBufferSize = 1024;
|
||||
|
||||
if (!WebApi.checkCredentialsReadonly(request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
auto stream = request->beginResponseStream("text/plain; charset=utf-8", 40960);
|
||||
auto stream = request->beginResponseStream("text/plain; charset=utf-8", initialResponseBufferSize);
|
||||
|
||||
stream->print("# HELP opendtu_build Build info\n");
|
||||
stream->print("# TYPE opendtu_build gauge\n");
|
||||
@@ -109,6 +111,10 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques
|
||||
}
|
||||
}
|
||||
stream->addHeader("Cache-Control", "no-cache");
|
||||
if (stream->available() > initialResponseBufferSize) {
|
||||
initialResponseBufferSize = stream->available();
|
||||
MessageOutput.printf("Increased /api/prometheus/metrics initialResponseBufferSize to %" PRIu32 " bytes\r\n", initialResponseBufferSize);
|
||||
}
|
||||
request->send(stream);
|
||||
|
||||
} catch (std::bad_alloc& bad_alloc) {
|
||||
|
||||
Reference in New Issue
Block a user