From f880d3b19ee98dfb2f09843401aeb2a30389cac1 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 7 Feb 2024 10:53:03 +0100 Subject: [PATCH] HttpServerConnection#StartStreaming(): release CpuBoundWork so that /v1/events doesn't have to use IoBoundWorkSlot. IoBoundWorkSlot#~IoBoundWorkSlot() will wait for a free semaphore slot which will be almost immediately released by CpuBoundWork#~CpuBoundWork(). Just releasing the already aquired slot in HttpServerConnection#StartStreaming() is more efficient. --- lib/remote/eventshandler.cpp | 2 -- lib/remote/httpserverconnection.cpp | 9 +++++++-- lib/remote/httpserverconnection.hpp | 2 ++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/remote/eventshandler.cpp b/lib/remote/eventshandler.cpp index bdda71461..20a331a8b 100644 --- a/lib/remote/eventshandler.cpp +++ b/lib/remote/eventshandler.cpp @@ -105,8 +105,6 @@ bool EventsHandler::HandleRequest( response.result(http::status::ok); response.set(http::field::content_type, "application/json"); - IoBoundWorkSlot dontLockTheIoThread (yc); - http::async_write(stream, response, yc); stream.async_flush(yc); diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index bb5831f4a..ed14fb30a 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -12,7 +12,6 @@ #include "base/configtype.hpp" #include "base/defer.hpp" #include "base/exception.hpp" -#include "base/io-engine.hpp" #include "base/logger.hpp" #include "base/objectlock.hpp" #include "base/timer.hpp" @@ -119,6 +118,9 @@ void HttpServerConnection::StartStreaming() m_HasStartedStreaming = true; + VERIFY(m_HandlingRequest); + m_HandlingRequest->Done(); + HttpServerConnection::Ptr keepAlive (this); IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) { @@ -432,6 +434,7 @@ bool ProcessRequest( ApiUser::Ptr& authenticatedUser, boost::beast::http::response& response, HttpServerConnection& server, + CpuBoundWork*& m_HandlingRequest, bool& hasStartedStreaming, boost::asio::yield_context& yc ) @@ -440,6 +443,8 @@ bool ProcessRequest( try { CpuBoundWork handlingRequest (yc); + Defer resetHandlingRequest ([&m_HandlingRequest] { m_HandlingRequest = nullptr; }); + m_HandlingRequest = &handlingRequest; HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, server); } catch (const std::exception& ex) { @@ -553,7 +558,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc) m_Seen = std::numeric_limits::max(); - if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, yc)) { + if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HandlingRequest, m_HasStartedStreaming, yc)) { break; } diff --git a/lib/remote/httpserverconnection.hpp b/lib/remote/httpserverconnection.hpp index 63f99e19c..8826705e0 100644 --- a/lib/remote/httpserverconnection.hpp +++ b/lib/remote/httpserverconnection.hpp @@ -4,6 +4,7 @@ #define HTTPSERVERCONNECTION_H #include "remote/apiuser.hpp" +#include "base/io-engine.hpp" #include "base/string.hpp" #include "base/tlsstream.hpp" #include @@ -40,6 +41,7 @@ private: boost::asio::io_context::strand m_IoStrand; bool m_ShuttingDown; bool m_HasStartedStreaming; + CpuBoundWork* m_HandlingRequest = nullptr; boost::asio::deadline_timer m_CheckLivenessTimer; HttpServerConnection(const String& identity, bool authenticated, const Shared::Ptr& stream, boost::asio::io_context& io);