Skip to content

Commit f85f9a3

Browse files
committed
HttpServerConnection: Log noticable CPU semaphore wait time
1 parent 73d3e5b commit f85f9a3

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/remote/httpserverconnection.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,17 @@ bool ProcessRequest(
430430
boost::beast::http::response<boost::beast::http::string_body>& response,
431431
HttpServerConnection& server,
432432
bool& hasStartedStreaming,
433+
std::chrono::steady_clock::duration& cpuBoundWorkTime,
433434
boost::asio::yield_context& yc
434435
)
435436
{
436437
namespace http = boost::beast::http;
437438

438439
try {
440+
// Cache the elapsed time to acquire a CPU semaphore used to detect extremely heavy workloads.
441+
auto start (std::chrono::steady_clock::now());
439442
CpuBoundWork handlingRequest (yc);
443+
cpuBoundWorkTime = std::chrono::steady_clock::now() - start;
440444

441445
HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, server);
442446
} catch (const std::exception& ex) {
@@ -527,9 +531,14 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
527531
<< ", user: " << (authenticatedUser ? authenticatedUser->GetName() : "<unauthenticated>")
528532
<< ", agent: " << request[http::field::user_agent]; //operator[] - Returns the value for a field, or "" if it does not exist.
529533

530-
Defer addRespCode ([&response, start, &logMsg]() {
531-
logMsg << ", status: " << response.result() << ") took "
532-
<< ch::duration_cast<ch::milliseconds>(ch::steady_clock::now() - start).count() << "ms.";
534+
ch::steady_clock::duration cpuBoundWorkTime(0);
535+
Defer addRespCode ([&response, start, &logMsg, &cpuBoundWorkTime]() {
536+
logMsg << ", status: " << response.result() << ")";
537+
if (cpuBoundWorkTime >= ch::seconds(1)) {
538+
logMsg << " waited " << ch::duration_cast<ch::milliseconds>(cpuBoundWorkTime).count() << "ms on semaphore and";
539+
}
540+
541+
logMsg << " took total " << ch::duration_cast<ch::milliseconds>(ch::steady_clock::now() - start).count() << "ms.";
533542
});
534543

535544
if (!HandleAccessControl(*m_Stream, request, response, yc)) {
@@ -550,7 +559,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
550559

551560
m_Seen = std::numeric_limits<decltype(m_Seen)>::max();
552561

553-
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, yc)) {
562+
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, cpuBoundWorkTime, yc)) {
554563
break;
555564
}
556565

0 commit comments

Comments
 (0)