Jelajahi Sumber

fix: clamp the form dispatch pool's thread/queue config instead of trusting it

server.form_dispatch_threads and server.form_dispatch_queue_capacity are read
as plain int from JSON config and were assigned straight into DispatchConfig
fields that are std::size_t. A configured 0 produced a pool with zero
workers - every immediate-mode submission would get its "thanks, your answer
was received" page with nothing ever going on to run it - and a negative
value wrapped to an enormous size on the int -> size_t conversion.

clampDispatchSetting() bounds both to a sane range (1-64 threads, 1-10000
queued) and logs when a configured value was out of range and got clamped,
rather than silently substituting a different number than the one in the
config file.
fszontagh 1 bulan lalu
induk
melakukan
998dc23f17
1 mengubah file dengan 25 tambahan dan 2 penghapusan
  1. 25 2
      src/webserver/webserver_service.cpp

+ 25 - 2
src/webserver/webserver_service.cpp

@@ -1,4 +1,5 @@
 #include "webserver_service.hpp"
+#include <algorithm>
 #include "api/auth_controller.hpp"
 #include "api/user_controller.hpp"
 #include "api/workflow_controller.hpp"
@@ -25,6 +26,26 @@
 
 namespace smartbotic::webserver {
 
+namespace {
+
+// server.form_dispatch_threads / server.form_dispatch_queue_capacity are read
+// as plain int from JSON config and handed to DispatchConfig fields that are
+// std::size_t: 0 threads means every immediate-mode form submission gets a
+// "thanks, your answer was received" that nothing will ever run, and a
+// negative value silently wraps to an enormous unsigned size on the
+// conversion. Clamp to a sane range and say so, rather than let either
+// mistake pass through as if it were what the operator meant.
+std::size_t clampDispatchSetting(const char* name, int configured, int min_value, int max_value) {
+    int clamped = std::clamp(configured, min_value, max_value);
+    if (clamped != configured) {
+        LOG_WARN("Configured {} of {} is out of the allowed range [{}, {}]; using {} instead",
+                 name, configured, min_value, max_value, clamped);
+    }
+    return static_cast<std::size_t>(clamped);
+}
+
+} // namespace
+
 WebServerService::WebServerService(const WebServerServiceConfig& config)
     : config_(config) {
 
@@ -417,8 +438,10 @@ void WebServerService::setupRoutes() {
     runner_ctrl_->registerRoutes(server);
 
     api::WebhookController::DispatchConfig form_dispatch_config;
-    form_dispatch_config.threads = config_.form_dispatch_threads;
-    form_dispatch_config.queue_capacity = config_.form_dispatch_queue_capacity;
+    form_dispatch_config.threads =
+        clampDispatchSetting("server.form_dispatch_threads", config_.form_dispatch_threads, 1, 64);
+    form_dispatch_config.queue_capacity = clampDispatchSetting(
+        "server.form_dispatch_queue_capacity", config_.form_dispatch_queue_capacity, 1, 10000);
     webhook_ctrl_ = std::make_unique<api::WebhookController>(
         *storage_, *runner_registry_, *load_balancer_, *ws_server_, *node_store_, *scheduler_,
         *jwt_, form_dispatch_config);