Skip to content

Commit e51fa06

Browse files
committed
HttpServerConnection: Log noticable CPU semaphore wait time
1 parent a51910a commit e51fa06

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/remote/httpserverconnection.cpp

Lines changed: 14 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,15 @@ 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+
538+
if (ch::duration_cast<ch::seconds>(cpuBoundWorkTime).count()) {
539+
logMsg << " waited '" << ch::duration_cast<ch::milliseconds>(cpuBoundWorkTime).count() << "ms' on semaphore and";
540+
}
541+
542+
logMsg << " took total " << ch::duration_cast<ch::milliseconds>(ch::steady_clock::now() - start).count() << "ms.";
533543
});
534544

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

551561
m_Seen = std::numeric_limits<decltype(m_Seen)>::max();
552562

553-
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, yc)) {
563+
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, cpuBoundWorkTime, yc)) {
554564
break;
555565
}
556566

0 commit comments

Comments
 (0)