Skip to content

Commit

Permalink
net::Defaults.maxTCPConnections: Use system's maximum concurrent TCP …
Browse files Browse the repository at this point in the history
…connections, disable if undefined

Used system value on a Linux kernel are
- /proc/sys/net/ipv4/tcp_max_orphans
  See https://www.kernel.org/doc/html/latest/networking/ip-sysctl.html
- /proc/sys/net/nf_conntrack_max
  See https://www.kernel.org/doc/html/latest/networking/nf_conntrack-sysctl.html

Signed-off-by: Sven Göthel <[email protected]>
Change-Id: Iad74f253bdac5636757b130b299b5deacda658db
  • Loading branch information
Sven Göthel committed Nov 1, 2024
1 parent 0e51fcf commit 342435e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
16 changes: 16 additions & 0 deletions common/Util-desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <config.h>

#include "Util.hpp"
#include "FileUtil.hpp"

#ifdef __linux__
#include <sys/time.h>
Expand Down Expand Up @@ -160,6 +161,21 @@ std::size_t getTotalSystemMemoryKb()
return totalMemKb;
}

std::size_t getMaxConcurrentTCPConnections()
{
#ifdef __linux__
char line[1024+1]; // includes EOS
const ssize_t tcp_max_orphans = FileUtil::readDecimal("/proc/sys/net/ipv4/tcp_max_orphans", line, sizeof(line), 0);
const ssize_t nf_conntrack_max = FileUtil::readDecimal("/proc/sys/net/nf_conntrack_max", line, sizeof(line), 0);
LOG_DBG("MaxConcurrentTCPConnections: min(orphans " << tcp_max_orphans
<< ", conntrack " << nf_conntrack_max << ") = "
<< std::min(tcp_max_orphans, nf_conntrack_max));
return std::min(tcp_max_orphans, nf_conntrack_max);
#else
return 0;
#endif
}

std::size_t getFromCGroup(const std::string& group, const std::string& key)
{
std::size_t num = 0;
Expand Down
1 change: 1 addition & 0 deletions common/Util-mobile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int spawnProcess(const std::string& cmd, const StringVector& args) { return 0; }

std::string getHumanizedBytes(unsigned long nBytes) { return std::string(); }
size_t getTotalSystemMemoryKb() { return 0; }
std::size_t getMaxConcurrentTCPConnections() { return 0; }
std::size_t getFromFile(const char* path) { return 0; }
std::size_t getCGroupMemLimit() { return 0; }
std::size_t getCGroupMemSoftLimit() { return 0; }
Expand Down
3 changes: 3 additions & 0 deletions common/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ namespace Util
/// Returns the total physical memory (in kB) available in the system
size_t getTotalSystemMemoryKb();

/// Returns the maximum number of concurrent TCP connections, zero if undefined.
std::size_t getMaxConcurrentTCPConnections();

/// Returns the numerical content of a file at @path
std::size_t getFromFile(const char *path);

Expand Down
2 changes: 1 addition & 1 deletion net/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ std::unique_ptr<Watchdog> SocketPoll::PollWatchdog;
std::atomic<size_t> StreamSocket::ExternalConnectionCount = 0;

net::DefaultValues net::Defaults = { .inactivityTimeout = std::chrono::seconds(3600),
.maxExtConnections = 200000 /* arbitrary value to be resolved */ };
.maxExtConnections = 0 /* unlimited default */};

#define SOCKET_ABSTRACT_UNIX_NAME "0coolwsd-"

Expand Down
3 changes: 3 additions & 0 deletions wsd/COOLWSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,9 @@ void COOLWSD::innerInitialize(Poco::Util::Application& self)
}
UnitWSD::get().setWSD(this);

// net::Defaults: Determine maxExtConnections field
net::Defaults.maxExtConnections = std::max(Util::getMaxConcurrentTCPConnections(), std::max<size_t>(3, MAX_CONNECTIONS));

// Allow UT to manipulate before using configuration values.
UnitWSD::get().configure(conf);

Expand Down

0 comments on commit 342435e

Please sign in to comment.