@@ -430,13 +430,17 @@ bool ProcessRequest(
430
430
boost::beast::http::response<boost::beast::http::string_body>& response,
431
431
HttpServerConnection& server,
432
432
bool & hasStartedStreaming,
433
+ std::chrono::steady_clock::duration& cpuBoundWorkTime,
433
434
boost::asio::yield_context& yc
434
435
)
435
436
{
436
437
namespace http = boost::beast::http;
437
438
438
439
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 ());
439
442
CpuBoundWork handlingRequest (yc);
443
+ cpuBoundWorkTime = std::chrono::steady_clock::now () - start;
440
444
441
445
HttpHandler::ProcessRequest (stream, authenticatedUser, request, response, yc, server);
442
446
} catch (const std::exception& ex) {
@@ -527,9 +531,14 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
527
531
<< " , user: " << (authenticatedUser ? authenticatedUser->GetName () : " <unauthenticated>" )
528
532
<< " , agent: " << request[http::field::user_agent]; // operator[] - Returns the value for a field, or "" if it does not exist.
529
533
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." ;
533
542
});
534
543
535
544
if (!HandleAccessControl (*m_Stream, request, response, yc)) {
@@ -550,7 +559,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
550
559
551
560
m_Seen = std::numeric_limits<decltype (m_Seen)>::max ();
552
561
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)) {
554
563
break ;
555
564
}
556
565
0 commit comments