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.
This commit is contained in:
Alexander A. Klimov 2024-02-07 10:53:03 +01:00
parent a13751d972
commit 3d5e031adf
3 changed files with 10 additions and 4 deletions

View File

@ -106,8 +106,6 @@ bool EventsHandler::HandleRequest(
response.result(http::status::ok); response.result(http::status::ok);
response.set(http::field::content_type, "application/json"); response.set(http::field::content_type, "application/json");
IoBoundWorkSlot dontLockTheIoThread (yc);
http::async_write(stream, response, yc); http::async_write(stream, response, yc);
stream.async_flush(yc); stream.async_flush(yc);

View File

@ -12,7 +12,6 @@
#include "base/configtype.hpp" #include "base/configtype.hpp"
#include "base/defer.hpp" #include "base/defer.hpp"
#include "base/exception.hpp" #include "base/exception.hpp"
#include "base/io-engine.hpp"
#include "base/logger.hpp" #include "base/logger.hpp"
#include "base/objectlock.hpp" #include "base/objectlock.hpp"
#include "base/timer.hpp" #include "base/timer.hpp"
@ -105,6 +104,9 @@ void HttpServerConnection::StartStreaming()
m_HasStartedStreaming = true; m_HasStartedStreaming = true;
VERIFY(m_HandlingRequest);
m_HandlingRequest->Done();
HttpServerConnection::Ptr keepAlive (this); HttpServerConnection::Ptr keepAlive (this);
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) { IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
@ -418,6 +420,7 @@ bool ProcessRequest(
ApiUser::Ptr& authenticatedUser, ApiUser::Ptr& authenticatedUser,
boost::beast::http::response<boost::beast::http::string_body>& response, boost::beast::http::response<boost::beast::http::string_body>& response,
HttpServerConnection& server, HttpServerConnection& server,
CpuBoundWork*& m_HandlingRequest,
bool& hasStartedStreaming, bool& hasStartedStreaming,
const WaitGroup::Ptr& waitGroup, const WaitGroup::Ptr& waitGroup,
std::chrono::steady_clock::duration& cpuBoundWorkTime, std::chrono::steady_clock::duration& cpuBoundWorkTime,
@ -432,6 +435,9 @@ bool ProcessRequest(
CpuBoundWork handlingRequest (yc); CpuBoundWork handlingRequest (yc);
cpuBoundWorkTime = std::chrono::steady_clock::now() - start; cpuBoundWorkTime = std::chrono::steady_clock::now() - start;
Defer resetHandlingRequest ([&m_HandlingRequest] { m_HandlingRequest = nullptr; });
m_HandlingRequest = &handlingRequest;
HttpHandler::ProcessRequest(waitGroup, stream, authenticatedUser, request, response, yc, server); HttpHandler::ProcessRequest(waitGroup, stream, authenticatedUser, request, response, yc, server);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
if (hasStartedStreaming) { if (hasStartedStreaming) {
@ -549,7 +555,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
m_Seen = std::numeric_limits<decltype(m_Seen)>::max(); m_Seen = std::numeric_limits<decltype(m_Seen)>::max();
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, m_WaitGroup, cpuBoundWorkTime, yc)) { if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HandlingRequest, m_HasStartedStreaming, m_WaitGroup, cpuBoundWorkTime, yc)) {
break; break;
} }

View File

@ -4,6 +4,7 @@
#define HTTPSERVERCONNECTION_H #define HTTPSERVERCONNECTION_H
#include "remote/apiuser.hpp" #include "remote/apiuser.hpp"
#include "base/io-engine.hpp"
#include "base/string.hpp" #include "base/string.hpp"
#include "base/tlsstream.hpp" #include "base/tlsstream.hpp"
#include "base/wait-group.hpp" #include "base/wait-group.hpp"
@ -42,6 +43,7 @@ private:
boost::asio::io_context::strand m_IoStrand; boost::asio::io_context::strand m_IoStrand;
bool m_ShuttingDown; bool m_ShuttingDown;
bool m_HasStartedStreaming; bool m_HasStartedStreaming;
CpuBoundWork* m_HandlingRequest = nullptr;
boost::asio::deadline_timer m_CheckLivenessTimer; boost::asio::deadline_timer m_CheckLivenessTimer;
HttpServerConnection(const WaitGroup::Ptr& waitGroup, const String& identity, bool authenticated, HttpServerConnection(const WaitGroup::Ptr& waitGroup, const String& identity, bool authenticated,