Implement connection limit
Now that we rely on normal steady_timer, bmcweb doesn't limit http
connections. This commit moves the connectionCount variable out of the
debug ifdefs, and into the "normal" build. Then additionally, add a
check to ensure that less than 100 connections are started at a time.
This count is intended to match the code in timer_queue.hpp that limited
this to 100 timers at a given time.
Tested:
/redfish/v1 returns properly.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I93ceaf8319d09d911b36cb7b21bba0cf64a9f7b8
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index bfd6411..06bb63a 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -35,9 +35,7 @@
res.addHeader("Content-Type", "text/html;charset=UTF-8");
}
-#ifdef BMCWEB_ENABLE_DEBUG
-static std::atomic<int> connectionCount;
-#endif
+static int connectionCount = 0;
// request body limit size set by the bmcwebHttpReqBodyLimitMb option
constexpr unsigned int httpReqBodyLimit =
@@ -67,22 +65,20 @@
prepareMutualTls();
#endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
-#ifdef BMCWEB_ENABLE_DEBUG
connectionCount++;
+
BMCWEB_LOG_DEBUG << this << " Connection open, total "
<< connectionCount;
-#endif
}
~Connection()
{
res.setCompleteRequestHandler(nullptr);
cancelDeadlineTimer();
-#ifdef BMCWEB_ENABLE_DEBUG
+
connectionCount--;
BMCWEB_LOG_DEBUG << this << " Connection closed, total "
<< connectionCount;
-#endif
}
void prepareMutualTls()
@@ -277,6 +273,12 @@
void start()
{
+ if (connectionCount >= 100)
+ {
+ BMCWEB_LOG_CRITICAL << this << "Max connection count exceeded.";
+ return;
+ }
+
startDeadline();
// TODO(ed) Abstract this to a more clever class with the idea of an