@@ -428,13 +428,17 @@ bool ProcessRequest(
428
428
boost::beast::http::response<boost::beast::http::string_body>& response,
429
429
HttpServerConnection& server,
430
430
bool & hasStartedStreaming,
431
+ std::chrono::steady_clock::duration& cpuBoundWorkTime,
431
432
boost::asio::yield_context& yc
432
433
)
433
434
{
434
435
namespace http = boost::beast::http;
435
436
436
437
try {
438
+ // Cache the elapsed time to acquire a CPU semaphore used to detect extremely heavy workloads.
439
+ auto start (std::chrono::steady_clock::now ());
437
440
CpuBoundWork handlingRequest (yc);
441
+ cpuBoundWorkTime = std::chrono::steady_clock::now () - start;
438
442
439
443
HttpHandler::ProcessRequest (stream, authenticatedUser, request, response, yc, server);
440
444
} catch (const std::exception& ex) {
@@ -525,9 +529,14 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
525
529
<< " , user: " << (authenticatedUser ? authenticatedUser->GetName () : " <unauthenticated>" )
526
530
<< " , agent: " << request[http::field::user_agent]; // operator[] - Returns the value for a field, or "" if it does not exist.
527
531
528
- Defer addRespCode ([&response, start, &logMsg]() {
529
- logMsg << " , status: " << response.result () << " ) took "
530
- << ch::duration_cast<ch::milliseconds>(ch::steady_clock::now () - start).count () << " ms." ;
532
+ ch::steady_clock::duration cpuBoundWorkTime (0 );
533
+ Defer addRespCode ([&response, start, &logMsg, &cpuBoundWorkTime]() {
534
+ logMsg << " , status: " << response.result () << " )" ;
535
+ if (cpuBoundWorkTime >= ch::seconds (1 )) {
536
+ logMsg << " waited " << ch::duration_cast<ch::milliseconds>(cpuBoundWorkTime).count () << " ms on semaphore and" ;
537
+ }
538
+
539
+ logMsg << " took total " << ch::duration_cast<ch::milliseconds>(ch::steady_clock::now () - start).count () << " ms." ;
531
540
});
532
541
533
542
if (!HandleAccessControl (*m_Stream, request, response, yc)) {
@@ -548,7 +557,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
548
557
549
558
m_Seen = std::numeric_limits<decltype (m_Seen)>::max ();
550
559
551
- if (!ProcessRequest (*m_Stream, request, authenticatedUser, response, *this , m_HasStartedStreaming, yc)) {
560
+ if (!ProcessRequest (*m_Stream, request, authenticatedUser, response, *this , m_HasStartedStreaming, cpuBoundWorkTime, yc)) {
552
561
break ;
553
562
}
554
563
0 commit comments