diff --git a/lib/remote/eventshandler.cpp b/lib/remote/eventshandler.cpp index 1b7798c04..8a2d59263 100644 --- a/lib/remote/eventshandler.cpp +++ b/lib/remote/eventshandler.cpp @@ -100,8 +100,6 @@ bool EventsHandler::HandleRequest( EventsSubscriber subscriber (std::move(eventTypes), HttpUtility::GetLastParameter(params, "filter"), l_ApiQuery); - IoBoundWorkSlot dontLockTheIoThread (yc); - response.result(http::status::ok); response.set(http::field::content_type, "application/json"); response.StartStreaming(true); diff --git a/lib/remote/httpmessage.cpp b/lib/remote/httpmessage.cpp index 18e5a3016..4a647dde3 100644 --- a/lib/remote/httpmessage.cpp +++ b/lib/remote/httpmessage.cpp @@ -151,6 +151,12 @@ void HttpResponse::Flush(boost::asio::yield_context yc) void HttpResponse::StartStreaming(bool checkForDisconnect) { + auto work (m_CpuBoundWork.lock()); + + if (work) { + work->Done(); + } + ASSERT(body().Size() == 0 && !m_SerializationStarted); body().Start(); chunked(true); diff --git a/lib/remote/httpmessage.hpp b/lib/remote/httpmessage.hpp index 10d00fd49..2376a6453 100644 --- a/lib/remote/httpmessage.hpp +++ b/lib/remote/httpmessage.hpp @@ -3,6 +3,7 @@ #pragma once #include "base/dictionary.hpp" +#include "base/io-engine.hpp" #include "base/json.hpp" #include "base/tlsstream.hpp" #include "remote/apiuser.hpp" @@ -10,6 +11,8 @@ #include "remote/url.hpp" #include #include +#include +#include namespace icinga { @@ -217,6 +220,11 @@ public: */ void Clear(); + void SetCpuBoundWork(std::weak_ptr cbw) + { + m_CpuBoundWork = std::move(cbw); + } + /** * Writes as much of the response as is currently available. * @@ -273,6 +281,7 @@ private: using Serializer = boost::beast::http::response_serializer; Serializer m_Serializer{*this}; bool m_SerializationStarted = false; + std::weak_ptr m_CpuBoundWork; HttpServerConnection::Ptr m_Server; Shared::Ptr m_Stream; diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index d8befd211..de2e46dd3 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -419,9 +419,10 @@ void ProcessRequest( try { // Cache the elapsed time to acquire a CPU semaphore used to detect extremely heavy workloads. auto start (std::chrono::steady_clock::now()); - CpuBoundWork handlingRequest (yc); + auto handlingRequest (std::make_shared(yc)); cpuBoundWorkTime = std::chrono::steady_clock::now() - start; + response.SetCpuBoundWork(handlingRequest); HttpHandler::ProcessRequest(waitGroup, request, response, yc); response.body().Finish(); } catch (const std::exception& ex) {