From: Remi Gacogne Date: Tue, 15 Apr 2025 14:39:43 +0000 (+0200) Subject: dnsdist: Refactor some very similar functions in the TCP limits code X-Git-Tag: dnsdist-2.0.0-alpha2~60^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c14c7fd50ee10fc1986999ffcc438313a4eb652a;p=thirdparty%2Fpdns.git dnsdist: Refactor some very similar functions in the TCP limits code --- diff --git a/pdns/dnsdistdist/dnsdist-concurrent-connections.cc b/pdns/dnsdistdist/dnsdist-concurrent-connections.cc index bf3169a168..c2f2c64657 100644 --- a/pdns/dnsdistdist/dnsdist-concurrent-connections.cc +++ b/pdns/dnsdistdist/dnsdist-concurrent-connections.cc @@ -269,12 +269,8 @@ void IncomingConcurrentTCPConnectionsManager::banClientFor(const ComboAddress& f vinfolog("Banned TCP client %s for %d seconds", from.toStringWithPort(), seconds); } -void IncomingConcurrentTCPConnectionsManager::accountClosedTCPConnection(const ComboAddress& from) +static void editEntryIfPresent(const ComboAddress& from, const std::function& callback) { - const auto maxConnsPerClient = dnsdist::configuration::getImmutableConfiguration().d_maxTCPConnectionsPerClient; - if (maxConnsPerClient == 0) { - return; - } auto addr = getRange(from); auto shardID = getShardID(addr); { @@ -283,47 +279,44 @@ void IncomingConcurrentTCPConnectionsManager::accountClosedTCPConnection(const C if (it == db->end()) { return; } - auto& count = it->d_concurrentConnections; - count--; + callback(*it); } } +void IncomingConcurrentTCPConnectionsManager::accountClosedTCPConnection(const ComboAddress& from) +{ + const auto maxConnsPerClient = dnsdist::configuration::getImmutableConfiguration().d_maxTCPConnectionsPerClient; + if (maxConnsPerClient == 0) { + return; + } + editEntryIfPresent(from, [](const ClientEntry& entry) { + auto& count = entry.d_concurrentConnections; + count--; + }); +} + void IncomingConcurrentTCPConnectionsManager::accountTLSNewSession(const ComboAddress& from) { - const auto maxRate = dnsdist::configuration::getImmutableConfiguration().d_maxTLSNewSessionsRatePerClient > 0; + const auto maxRate = dnsdist::configuration::getImmutableConfiguration().d_maxTLSNewSessionsRatePerClient; if (maxRate == 0) { return; } - auto addr = getRange(from); - auto shardID = getShardID(addr); - { - auto db = s_tcpClientsConnectionMetrics.at(shardID).lock(); - auto it = db->find(addr); - if (it == db->end()) { - return; - } - auto& count = getCurrentClientActivity(*it, time(nullptr)).tlsNewSessions; + editEntryIfPresent(from, [](const ClientEntry& entry) { + auto& count = getCurrentClientActivity(entry, time(nullptr)).tlsNewSessions; count++; - } + }); } void IncomingConcurrentTCPConnectionsManager::accountTLSResumedSession(const ComboAddress& from) { - const auto maxRate = dnsdist::configuration::getImmutableConfiguration().d_maxTLSResumedSessionsRatePerClient > 0; + const auto maxRate = dnsdist::configuration::getImmutableConfiguration().d_maxTLSResumedSessionsRatePerClient; if (maxRate == 0) { return; } - auto addr = getRange(from); - auto shardID = getShardID(addr); - { - auto db = s_tcpClientsConnectionMetrics.at(shardID).lock(); - auto it = db->find(addr); - if (it == db->end()) { - return; - } - auto& count = getCurrentClientActivity(*it, time(nullptr)).tlsResumedSessions; + editEntryIfPresent(from, [](const ClientEntry& entry) { + auto& count = getCurrentClientActivity(entry, time(nullptr)).tlsResumedSessions; count++; - } + }); } }