changeset 6263:fb8bed996341

allow HttpServer to start without a MetricsRegistry
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 11 Aug 2025 08:56:23 +0200
parents dbfcecfb5f60
children de176f458e35
files OrthancFramework/Sources/HttpServer/HttpServer.cpp OrthancFramework/Sources/HttpServer/HttpServer.h OrthancFramework/UnitTestsSources/RestApiTests.cpp OrthancServer/Sources/main.cpp
diffstat 4 files changed, 45 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Mon Aug 11 08:25:31 2025 +0200
+++ b/OrthancFramework/Sources/HttpServer/HttpServer.cpp	Mon Aug 11 08:56:23 2025 +0200
@@ -32,7 +32,6 @@
 #include "../Logging.h"
 #include "../OrthancException.h"
 #include "../TemporaryFile.h"
-#include "../MetricsRegistry.h"
 #include "HttpToolbox.h"
 #include "IHttpHandler.h"
 #include "MultipartStreamReader.h"
@@ -1195,7 +1194,7 @@
   {
     bool localhost;
 
-    MetricsRegistry::AvailableResourcesDecounter counter(server.GetAvailableHttpThreadsMetrics());
+    std::unique_ptr<MetricsRegistry::AvailableResourcesDecounter> counter(server.CreateAvailableHttpThreadsDecounter());
 
 #if ORTHANC_ENABLE_MONGOOSE == 1
     static const long LOCALHOST = (127ll << 24) + 1ll;
@@ -1665,7 +1664,7 @@
   }
 
 
-  HttpServer::HttpServer(MetricsRegistry& metricsRegistry) :
+  HttpServer::HttpServer() :
     pimpl_(new PImpl),
     handler_(NULL),
     remoteAllowed_(false),
@@ -1683,10 +1682,7 @@
     realm_(ORTHANC_REALM),
     threadsCount_(50),  // Default value in mongoose/civetweb
     tcpNoDelay_(true),
-    requestTimeout_(30),  // Default value in mongoose/civetweb (30 seconds)
-    availableHttpThreadsMetrics_(metricsRegistry,
-                                 "orthanc_available_http_threads_count",
-                                 MetricsUpdatePolicy_MinOver10Seconds)
+    requestTimeout_(30)  // Default value in mongoose/civetweb (30 seconds)
   {
 #if ORTHANC_ENABLE_MONGOOSE == 1
     CLOG(INFO, HTTP) << "This Orthanc server uses Mongoose as its embedded HTTP server";
@@ -2203,7 +2199,11 @@
     
     Stop();
     threadsCount_ = threads;
-    availableHttpThreadsMetrics_.SetInitialValue(threadsCount_);
+
+    if (availableHttpThreadsMetrics_.get() != NULL)
+    {
+      availableHttpThreadsMetrics_->SetInitialValue(threadsCount_);
+    }
 
     CLOG(INFO, HTTP) << "The embedded HTTP server will use " << threads << " threads";
   }
@@ -2290,4 +2290,29 @@
     }
   }
 #endif
+
+
+  void HttpServer::SetMetricsRegistry(MetricsRegistry& metricsRegistry)
+  {
+    Stop();
+    availableHttpThreadsMetrics_.reset(new MetricsRegistry::SharedMetrics(
+                                         metricsRegistry,
+                                         "orthanc_available_http_threads_count",
+                                         MetricsUpdatePolicy_MinOver10Seconds));
+    availableHttpThreadsMetrics_->SetInitialValue(threadsCount_);
+  }
+
+
+  MetricsRegistry::AvailableResourcesDecounter* HttpServer::CreateAvailableHttpThreadsDecounter()
+  {
+    // NB: "availableHttpThreadsMetrics_" is protected by the mutex in "mg_stop()"
+    if (availableHttpThreadsMetrics_.get() != NULL)
+    {
+      return new MetricsRegistry::AvailableResourcesDecounter(*availableHttpThreadsMetrics_);
+    }
+    else
+    {
+      return NULL;
+    }
+  }
 }
--- a/OrthancFramework/Sources/HttpServer/HttpServer.h	Mon Aug 11 08:25:31 2025 +0200
+++ b/OrthancFramework/Sources/HttpServer/HttpServer.h	Mon Aug 11 08:56:23 2025 +0200
@@ -115,7 +115,7 @@
     unsigned int threadsCount_;
     bool tcpNoDelay_;
     unsigned int requestTimeout_;  // In seconds
-    MetricsRegistry::SharedMetrics availableHttpThreadsMetrics_;
+    std::unique_ptr<MetricsRegistry::SharedMetrics> availableHttpThreadsMetrics_;  // New in Orthanc 1.12.9
 
 #if ORTHANC_ENABLE_PUGIXML == 1
     WebDavBuckets webDavBuckets_;
@@ -124,7 +124,7 @@
     bool IsRunning() const;
 
   public:
-    explicit HttpServer(MetricsRegistry& metricsRegistry);
+    HttpServer();
 
     ~HttpServer();
 
@@ -230,11 +230,11 @@
                                   const std::string& boundary,
                                   const std::string& authenticationPayload);
 
-    MetricsRegistry::SharedMetrics& GetAvailableHttpThreadsMetrics()
-    {
-      return availableHttpThreadsMetrics_;
-    }
+    static std::string GetRelativePathToRoot(const std::string& uri);
 
-    static std::string GetRelativePathToRoot(const std::string& uri);
+    void SetMetricsRegistry(MetricsRegistry& metricsRegistry);
+
+    // Can return NULL if SetMetricsRegistry() was not call beforehand
+    MetricsRegistry::AvailableResourcesDecounter* CreateAvailableHttpThreadsDecounter();
   };
 }
--- a/OrthancFramework/UnitTestsSources/RestApiTests.cpp	Mon Aug 11 08:25:31 2025 +0200
+++ b/OrthancFramework/UnitTestsSources/RestApiTests.cpp	Mon Aug 11 08:56:23 2025 +0200
@@ -1333,9 +1333,8 @@
 TEST(HttpClient, DISABLED_Issue156_Slow)
 {
   // https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=156
-  MetricsRegistry dummyRegistry;
   TotoServer handler;
-  HttpServer server(dummyRegistry);
+  HttpServer server;
   server.SetPortNumber(5000);
   server.Register(handler);
   server.Start();
@@ -1362,9 +1361,8 @@
 
 TEST(HttpClient, DISABLED_Issue156_Crash)
 {
-  MetricsRegistry dummyRegistry;
   TotoServer handler;
-  HttpServer server(dummyRegistry);
+  HttpServer server;
   server.SetPortNumber(5000);
   server.Register(handler);
   server.Start();
--- a/OrthancServer/Sources/main.cpp	Mon Aug 11 08:25:31 2025 +0200
+++ b/OrthancServer/Sources/main.cpp	Mon Aug 11 08:56:23 2025 +0200
@@ -1049,7 +1049,7 @@
   else
   {
     MyIncomingHttpRequestFilter httpFilter(context, plugins);
-    HttpServer httpServer(context.GetMetricsRegistry());
+    HttpServer httpServer;
     bool httpDescribeErrors;
 
 #if ORTHANC_ENABLE_MONGOOSE == 1
@@ -1060,6 +1060,8 @@
 #  error "Either Mongoose or Civetweb must be enabled to compile this file"
 #endif
   
+    httpServer.SetMetricsRegistry(context.GetMetricsRegistry());
+
     {
       OrthancConfiguration::ReaderLock lock;