OutgoingHttpMessage: don't use shared_ptr for m_CpuBoundWork

This change just gives clear ownership over the CpuBoundWork to the
OutgoingHttpMessage, instead of the previous shared_ptr and weak_ptr
combination with an unclear purpose.
This commit is contained in:
Julian Brost 2026-03-24 17:52:33 +01:00
parent 85b16d2186
commit ed403294a3
3 changed files with 14 additions and 9 deletions

View file

@ -146,10 +146,7 @@ void OutgoingHttpMessage<isRequest, Body, StreamVariant>::Clear()
template<bool isRequest, typename Body, typename StreamVariant>
void OutgoingHttpMessage<isRequest, Body, StreamVariant>::Flush(boost::asio::yield_context yc, bool finish)
{
if (auto work (m_CpuBoundWork.lock()); work) {
work->Done();
m_CpuBoundWork.reset();
}
m_CpuBoundWork.reset();
if (!Base::chunked() && !Base::has_content_length()) {
ASSERT(!m_SerializationStarted);

View file

@ -267,15 +267,24 @@ public:
JsonEncoder GetJsonEncoder(bool pretty = false);
void SetCpuBoundWork(std::weak_ptr<CpuBoundWork> cbw)
/**
* Acquire a CpuBoundWork slot
*
* It is automatically released the next time data is written using Flush() (when doing IO, it's no longer
* CPU-bound), or when the object is destroyed.
*
* @param yc Yield context that is used for waiting.
* @param strand Strand the caller is running on, used for synchronization.
*/
void StartCpuBoundWork(boost::asio::yield_context yc, boost::asio::io_context::strand& strand)
{
m_CpuBoundWork = std::move(cbw);
m_CpuBoundWork.emplace(yc, strand);
}
private:
Serializer m_Serializer{*this};
bool m_SerializationStarted = false;
std::weak_ptr<CpuBoundWork> m_CpuBoundWork;
std::optional<CpuBoundWork> m_CpuBoundWork;
StreamVariant m_Stream;
};

View file

@ -425,10 +425,9 @@ 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());
auto handlingRequest (std::make_shared<CpuBoundWork>(yc, strand));
response.StartCpuBoundWork(yc, strand);
cpuBoundWorkTime = std::chrono::steady_clock::now() - start;
response.SetCpuBoundWork(handlingRequest);
HttpHandler::ProcessRequest(waitGroup, request, response, yc);
response.body().Finish();
} catch (const std::exception& ex) {