diff OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 3926:2910b0d30fe0 transcoding

Allow concurrent calls to the custom image decoders provided by the plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 14 May 2020 07:37:44 +0200
parents 09f091b6b569
children 4cdc875510d1
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Wed May 13 18:12:34 2020 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Thu May 14 07:37:44 2020 +0200
@@ -43,6 +43,7 @@
 #include "../../Core/Images/Image.h"
 #include "../../Core/Images/ImageProcessing.h"
 #include "../../Core/Logging.h"
+#include "../../Core/MultiThreading/Semaphore.h"
 #include "../DefaultDicomImageDecoder.h"
 #include "../OrthancConfiguration.h"
 #include "../Search/DatabaseLookup.h"
@@ -56,6 +57,14 @@
 #include <boost/math/special_functions/round.hpp>
 
 
+/**
+ * This semaphore is used to limit the number of concurrent HTTP
+ * requests on CPU-intensive routes of the REST API, in order to
+ * prevent exhaustion of resources (new in Orthanc 1.7.0).
+ **/
+static Orthanc::Semaphore throttlingSemaphore_(4);  // TODO => PARAMETER?
+
+
 namespace Orthanc
 {
   static void AnswerDicomAsJson(RestApiCall& call,
@@ -938,6 +947,8 @@
   template <enum ImageExtractionMode mode>
   static void GetImage(RestApiGetCall& call)
   {
+    Semaphore::Locker locker(throttlingSemaphore_);
+        
     GetImageHandler handler(mode);
     IDecodedFrameHandler::Apply(call, handler);
   }
@@ -945,6 +956,8 @@
 
   static void GetRenderedFrame(RestApiGetCall& call)
   {
+    Semaphore::Locker locker(throttlingSemaphore_);
+        
     RenderedFrameHandler handler;
     IDecodedFrameHandler::Apply(call, handler);
   }
@@ -952,6 +965,8 @@
 
   static void GetMatlabImage(RestApiGetCall& call)
   {
+    Semaphore::Locker locker(throttlingSemaphore_);
+        
     ServerContext& context = OrthancRestApi::GetContext(call);
 
     std::string frameId = call.GetUriComponent("frame", "0");