Skip to content

Commit

Permalink
dnfdaemon: Limit number of simultaneously active sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
m-blaha committed Feb 12, 2024
1 parent aad06db commit 59fcba3
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dnf5daemon-server/session_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <sdbus-c++/sdbus-c++.h>

#include <iostream>
#include <numeric>
#include <random>
#include <sstream>
#include <string>
#include <thread>

// TODO(mblaha): Make this constant configurable
const int MAX_SESSIONS = 3;

SessionManager::SessionManager() {
connection = sdbus::createSystemBusConnection(dnfdaemon::DBUS_NAME);
dbus_register();
Expand Down Expand Up @@ -98,6 +102,14 @@ sdbus::MethodReply SessionManager::open_session(sdbus::MethodCall & call) {
if (!active) {
throw sdbus::Error(dnfdaemon::ERROR, "Cannot open new session.");
}
// limit number of simultaneously opened sessions
const int num_sessions = std::accumulate(
sessions.begin(), sessions.end(), 0, [](int sum, const auto & sender) { return sum + sender.second.size(); });
if (num_sessions >= MAX_SESSIONS) {
auto reply = call.createErrorReply(sdbus::Error(
dnfdaemon::ERROR, "Cannot open new session - maximal number of simultaneously opened sessions achieved."));
return reply;
}

auto sender = call.getSender();
dnfdaemon::KeyValueMap configuration;
Expand Down

0 comments on commit 59fcba3

Please sign in to comment.