diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-02-10 19:34:47 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-02-10 19:34:47 +0100 |
commit | 00f02b19ad8ce2f8f0195d3610e06566bf68cd0c (patch) | |
tree | bf35dbcd980505ba9bc3404edb0921966b51cecf | |
parent | cba42916126a1baed33a6f122a5301982993d344 (diff) |
Added connection limit
-rw-r--r-- | config.cpp | 12 | ||||
-rw-r--r-- | config.h | 3 | ||||
-rw-r--r-- | debian/changelog | 6 | ||||
-rw-r--r-- | debian/whiteboard.conf | 8 | ||||
-rw-r--r-- | whiteboard.conf | 8 | ||||
-rw-r--r-- | whiteboard.cpp | 7 |
6 files changed, 42 insertions, 2 deletions
@@ -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<int>("config.threads", default_threads); + + m_max_connections = tree.get<int>("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; +} + @@ -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 <mail@reichwein.it> 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 --> <threads>4</threads> + + <!-- + Maximum number of total concurrent websocket connections. Above this limit, + new connections will be rejected until old ones are closed. + Example: 500 + Default: 1000 + --> + <maxconnections>500</maxconnections> </config> diff --git a/whiteboard.conf b/whiteboard.conf index 055e7ba..4622db9 100644 --- a/whiteboard.conf +++ b/whiteboard.conf @@ -23,4 +23,12 @@ Example: 4 --> <threads>4</threads> + + <!-- + Maximum number of total concurrent websocket connections. Above this limit, + new connections will be rejected until old ones are closed. + Example: 500 + Default: 1000 + --> + <maxconnections>500</maxconnections> </config> 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<session>(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<session>(m_registry, *m_storage, m_storage_mutex, std::move(socket))->run(); + } } do_accept(); |