From 00f02b19ad8ce2f8f0195d3610e06566bf68cd0c Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 10 Feb 2023 19:34:47 +0100 Subject: Added connection limit --- config.cpp | 12 +++++++++++- config.h | 3 +++ debian/changelog | 6 ++++++ debian/whiteboard.conf | 8 ++++++++ whiteboard.conf | 8 ++++++++ whiteboard.cpp | 7 ++++++- 6 files changed, 42 insertions(+), 2 deletions(-) diff --git a/config.cpp b/config.cpp index d59156f..afb5cd2 100644 --- a/config.cpp +++ b/config.cpp @@ -14,6 +14,7 @@ namespace { const uint64_t default_maxage{0}; // timeout in seconds; 0 = no timeout const std::string default_listen {"::1:9000"}; const int default_threads{1}; + const int default_max_connections{1000}; } Config::Config(const std::string& config_filename): @@ -21,7 +22,8 @@ Config::Config(const std::string& config_filename): m_maxage{default_maxage}, m_listenAddress{"::1"}, m_listenPort{9000}, - m_threads{default_threads} + m_threads{default_threads}, + m_max_connections{default_max_connections} { try { @@ -42,6 +44,8 @@ Config::Config(const std::string& config_filename): throw std::runtime_error("Bad listen port: "s + std::to_string(m_listenPort)); m_threads = tree.get("config.threads", default_threads); + + m_max_connections = tree.get("config.maxconnections", default_max_connections); } catch (const std::exception& ex) { std::cerr << "Error reading config file " << config_filename << ". Using defaults." << std::endl; } @@ -71,3 +75,9 @@ int Config::getThreads() const { return m_threads; } + +int Config::getMaxConnections() const +{ + return m_max_connections; +} + diff --git a/config.h b/config.h index 01310aa..ce2fceb 100644 --- a/config.h +++ b/config.h @@ -12,6 +12,7 @@ private: std::string m_listenAddress; // ip address v4/v6 int m_listenPort; int m_threads; + int m_max_connections; public: Config(const std::string& config_filename = default_config_filename); @@ -22,4 +23,6 @@ public: int getListenPort() const; int getThreads() const; + + int getMaxConnections() const; }; diff --git a/debian/changelog b/debian/changelog index d773faf..c0cb704 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +whiteboard (1.8) UNRELEASED; urgency=medium + + * Added config.maxconnections, defaults to 1000 + + -- Roland Reichwein Fri, 10 Feb 2023 19:18:16 +0100 + whiteboard (1.7) unstable; urgency=medium * Fix crash on stats.html page reload/close diff --git a/debian/whiteboard.conf b/debian/whiteboard.conf index 126bef5..6446d39 100644 --- a/debian/whiteboard.conf +++ b/debian/whiteboard.conf @@ -23,4 +23,12 @@ Example: 4 --> 4 + + + 500 diff --git a/whiteboard.conf b/whiteboard.conf index 055e7ba..4622db9 100644 --- a/whiteboard.conf +++ b/whiteboard.conf @@ -23,4 +23,12 @@ Example: 4 --> 4 + + + 500 diff --git a/whiteboard.cpp b/whiteboard.cpp index 6a71d3b..044321b 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -437,7 +437,12 @@ void Whiteboard::on_accept(boost::system::error_code ec, boost::asio::ip::tcp::s if (ec) { std::cerr << "Error on accept: " << ec.message() << std::endl; } else { - std::make_shared(m_registry, *m_storage, m_storage_mutex, std::move(socket))->run(); + if (m_registry.number_of_connections() >= m_config->getMaxConnections()) { + // limit reached + socket.close(); + } else { + std::make_shared(m_registry, *m_storage, m_storage_mutex, std::move(socket))->run(); + } } do_accept(); -- cgit v1.2.3