From 6fbdbcabc3ae43dc372459c6c5ae4d53657267f5 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Mon, 6 Dec 2021 14:36:06 -0800 Subject: 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 Change-Id: I93ceaf8319d09d911b36cb7b21bba0cf64a9f7b8 --- http/http_connection.hpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'http/http_connection.hpp') diff --git a/http/http_connection.hpp b/http/http_connection.hpp index bfd641154e..06bb63af04 100644 --- a/http/http_connection.hpp +++ b/http/http_connection.hpp @@ -35,9 +35,7 @@ inline void prettyPrintJson(crow::Response& res) res.addHeader("Content-Type", "text/html;charset=UTF-8"); } -#ifdef BMCWEB_ENABLE_DEBUG -static std::atomic connectionCount; -#endif +static int connectionCount = 0; // request body limit size set by the bmcwebHttpReqBodyLimitMb option constexpr unsigned int httpReqBodyLimit = @@ -67,22 +65,20 @@ class Connection : 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 @@ class Connection : 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 -- cgit v1.2.3