Skip to content

Commit 92f7113

Browse files
author
Sven Göthel
committed
Socket MaxTCPConnections Limiter (New Connections): Use system's maximum concurrent TCP 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 Previous confusion about 'max connections' semantics has been resolved, as this `net::Default::maxTCPConnections` is unrelated to session count but actual outfacing TCP connections only. Signed-off-by: Sven Göthel <[email protected]> Change-Id: Iad74f253bdac5636757b130b299b5deacda658db
1 parent 4437a49 commit 92f7113

File tree

8 files changed

+30
-13
lines changed

8 files changed

+30
-13
lines changed

common/Util-desktop.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <config.h>
1212

1313
#include "Util.hpp"
14+
#include "FileUtil.hpp"
1415

1516
#ifdef __linux__
1617
#include <sys/time.h>
@@ -160,6 +161,21 @@ std::size_t getTotalSystemMemoryKb()
160161
return totalMemKb;
161162
}
162163

164+
std::size_t getMaxConcurrentTCPConnections()
165+
{
166+
#ifdef __linux__
167+
char line[1024+1]; // includes EOS
168+
const ssize_t tcp_max_orphans = FileUtil::readDecimal("/proc/sys/net/ipv4/tcp_max_orphans", line, sizeof(line)-1, 0);
169+
const ssize_t nf_conntrack_max = FileUtil::readDecimal("/proc/sys/net/nf_conntrack_max", line, sizeof(line)-1, 0);
170+
LOG_DBG("MaxConcurrentTCPConnections: min(orphans " << tcp_max_orphans
171+
<< ", conntrack " << nf_conntrack_max << ") = "
172+
<< std::min(tcp_max_orphans, nf_conntrack_max));
173+
return std::min(tcp_max_orphans, nf_conntrack_max);
174+
#else
175+
return 0;
176+
#endif
177+
}
178+
163179
std::size_t getFromCGroup(const std::string& group, const std::string& key)
164180
{
165181
std::size_t num = 0;

common/Util-mobile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ int spawnProcess(const std::string& cmd, const StringVector& args) { return 0; }
2121

2222
std::string getHumanizedBytes(unsigned long nBytes) { return std::string(); }
2323
size_t getTotalSystemMemoryKb() { return 0; }
24+
std::size_t getMaxConcurrentTCPConnections() { return 0; }
2425
std::size_t getFromFile(const char* path) { return 0; }
2526
std::size_t getCGroupMemLimit() { return 0; }
2627
std::size_t getCGroupMemSoftLimit() { return 0; }

common/Util.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ namespace Util
353353
/// Returns the total physical memory (in kB) available in the system
354354
size_t getTotalSystemMemoryKb();
355355

356+
/// Returns the maximum number of concurrent TCP connections, zero if undefined.
357+
std::size_t getMaxConcurrentTCPConnections();
358+
356359
/// Returns the numerical content of a file at @path
357360
std::size_t getFromFile(const char *path);
358361

net/NetUtil.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ class DefaultValues
3636
/// StreamSocket inactivity timeout in us (3600s default). Zero disables instrument.
3737
std::chrono::microseconds inactivityTimeout;
3838

39-
/// Maximum total connections (9999 or MAX_CONNECTIONS). Zero disables instrument.
40-
size_t maxConnections;
39+
/// Maximum number of concurrent TCP connections. Zero disables instrument.
40+
size_t maxTCPConnections;
4141

4242
std::ostream& stream(std::ostream& os) const
4343
{
44-
os << "Socket[MaxConnections " << maxConnections
45-
<< "], Inactivity[timeout "
44+
os << "Socket[maxTCPConnections " << maxTCPConnections
45+
<< "], Inactivity-timeout "
4646
<< std::setw(5)
47-
<< inactivityTimeout.count() / 1000.0 << "ms]";
47+
<< inactivityTimeout.count() / 1000.0 << "ms";
4848
return os;
4949
}
5050

net/Socket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ std::mutex SocketPoll::_statsMutex;
6666
std::atomic<size_t> SocketPoll::_statsConnectionCount(0);
6767

6868
net::DefaultValues net::Defaults = { .inactivityTimeout = std::chrono::seconds(3600),
69-
.maxConnections = 9999 };
69+
.maxTCPConnections = 0 /* undefined default */};
7070

7171
size_t SocketPoll::statsConnectionMod(size_t added, size_t removed) {
7272
if( added == 0 && removed == 0 ) {

test/UnitTimeoutConnections.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UnitTimeoutConnections : public UnitTimeoutBase1
3434
{
3535
void configure(Poco::Util::LayeredConfiguration& /* config */) override
3636
{
37-
net::Defaults.maxConnections = ConnectionLimit;
37+
net::Defaults.maxTCPConnections = ConnectionLimit;
3838
}
3939

4040
public:

wsd/COOLWSD.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ void COOLWSD::innerInitialize(Poco::Util::Application& self)
23362336
UnitWSD::get().setWSD(this);
23372337

23382338
// net::Defaults: Set MaxConnections field
2339-
net::Defaults.maxConnections = std::max<size_t>(3, MAX_CONNECTIONS);
2339+
net::Defaults.maxTCPConnections = std::max(Util::getMaxConcurrentTCPConnections(), std::max<size_t>(3, MAX_CONNECTIONS));
23402340

23412341
// Allow UT to manipulate before using configuration values.
23422342
UnitWSD::get().configure(conf);
@@ -2747,19 +2747,16 @@ void COOLWSD::innerInitialize(Poco::Util::Application& self)
27472747
if (getConfigValue<bool>(conf, "home_mode.enable", false))
27482748
{
27492749
COOLWSD::MaxConnections = 20;
2750-
net::Defaults.maxConnections = COOLWSD::MaxConnections; // re-align
27512750
COOLWSD::MaxDocuments = 10;
27522751
}
27532752
else
27542753
{
27552754
conf.setString("feedback.show", "true");
27562755
conf.setString("welcome.enable", "true");
2757-
COOLWSD::MaxConnections = net::Defaults.maxConnections; // aligned w/ MAX_CONNECTIONS above
27582756
COOLWSD::MaxDocuments = MAX_DOCUMENTS;
27592757
}
27602758
#else
27612759
{
2762-
COOLWSD::MaxConnections = net::Defaults.maxConnections; // aligned w/ MAX_CONNECTIONS above
27632760
COOLWSD::MaxDocuments = MAX_DOCUMENTS;
27642761
}
27652762
#endif
@@ -2935,7 +2932,7 @@ void COOLWSD::innerInitialize(Poco::Util::Application& self)
29352932
#endif
29362933

29372934
WebServerPoll = std::make_unique<TerminatingPoll>("websrv_poll");
2938-
WebServerPoll->setLimiter( net::Defaults.maxConnections );
2935+
WebServerPoll->setLimiter( net::Defaults.maxTCPConnections ); // enabled if `maxTCPConnections` > 0
29392936

29402937
#if !MOBILEAPP
29412938
net::AsyncDNS::startAsyncDNS();

wsd/DocumentBroker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class DocumentBroker::DocumentBrokerPoll final : public TerminatingPoll
140140
TerminatingPoll(threadName),
141141
_docBroker(docBroker)
142142
{
143-
setLimiter( net::Defaults.maxConnections );
143+
setLimiter( net::Defaults.maxTCPConnections ); // enabled if `maxTCPConnections` > 0
144144
}
145145

146146
void pollingThread() override

0 commit comments

Comments
 (0)