Skip to content

Commit ab922b4

Browse files
committed
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.
1 parent 1da7ac6 commit ab922b4

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

lib/remote/eventshandler.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ bool EventsHandler::HandleRequest(
105105
response.result(http::status::ok);
106106
response.set(http::field::content_type, "application/json");
107107

108-
IoBoundWorkSlot dontLockTheIoThread (yc);
109-
110108
http::async_write(stream, response, yc);
111109
stream.async_flush(yc);
112110

lib/remote/httpserverconnection.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "base/configtype.hpp"
1313
#include "base/defer.hpp"
1414
#include "base/exception.hpp"
15-
#include "base/io-engine.hpp"
1615
#include "base/logger.hpp"
1716
#include "base/objectlock.hpp"
1817
#include "base/timer.hpp"
@@ -105,6 +104,9 @@ void HttpServerConnection::StartStreaming()
105104

106105
m_HasStartedStreaming = true;
107106

107+
VERIFY(m_HandlingRequest);
108+
m_HandlingRequest->Done();
109+
108110
HttpServerConnection::Ptr keepAlive (this);
109111

110112
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
@@ -418,6 +420,7 @@ bool ProcessRequest(
418420
ApiUser::Ptr& authenticatedUser,
419421
boost::beast::http::response<boost::beast::http::string_body>& response,
420422
HttpServerConnection& server,
423+
CpuBoundWork*& m_HandlingRequest,
421424
bool& hasStartedStreaming,
422425
std::chrono::steady_clock::duration& cpuBoundWorkTime,
423426
boost::asio::yield_context& yc
@@ -431,6 +434,9 @@ bool ProcessRequest(
431434
CpuBoundWork handlingRequest (yc);
432435
cpuBoundWorkTime = std::chrono::steady_clock::now() - start;
433436

437+
Defer resetHandlingRequest ([&m_HandlingRequest] { m_HandlingRequest = nullptr; });
438+
m_HandlingRequest = &handlingRequest;
439+
434440
HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, server);
435441
} catch (const std::exception& ex) {
436442
if (hasStartedStreaming) {
@@ -548,7 +554,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
548554

549555
m_Seen = std::numeric_limits<decltype(m_Seen)>::max();
550556

551-
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, cpuBoundWorkTime, yc)) {
557+
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HandlingRequest, m_HasStartedStreaming, cpuBoundWorkTime, yc)) {
552558
break;
553559
}
554560

lib/remote/httpserverconnection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define HTTPSERVERCONNECTION_H
55

66
#include "remote/apiuser.hpp"
7+
#include "base/io-engine.hpp"
78
#include "base/string.hpp"
89
#include "base/tlsstream.hpp"
910
#include <memory>
@@ -40,6 +41,7 @@ class HttpServerConnection final : public Object
4041
boost::asio::io_context::strand m_IoStrand;
4142
bool m_ShuttingDown;
4243
bool m_HasStartedStreaming;
44+
CpuBoundWork* m_HandlingRequest = nullptr;
4345
boost::asio::deadline_timer m_CheckLivenessTimer;
4446

4547
HttpServerConnection(const String& identity, bool authenticated, const Shared<AsioTlsStream>::Ptr& stream, boost::asio::io_context& io);

0 commit comments

Comments
 (0)