]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Convert the TCP client counts map to LockGuarded
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 30 Apr 2021 13:48:58 +0000 (15:48 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Aug 2021 12:04:45 +0000 (14:04 +0200)
pdns/dnsdist-tcp.cc

index cf253caf7ca37c01a14e1bd41cf1d4afc25548a0..0a7da953723aa92a36268dc2d28f0e8ea22fcc78 100644 (file)
@@ -55,8 +55,7 @@
    Let's start naively.
 */
 
-static std::mutex s_tcpClientsCountMutex;
-static std::map<ComboAddress,size_t,ComboAddress::addressOnlyLessThan> s_tcpClientsCount;
+static LockGuarded<std::map<ComboAddress,size_t,ComboAddress::addressOnlyLessThan>> s_tcpClientsCount;
 
 size_t g_maxTCPQueriesPerConn{0};
 size_t g_maxTCPConnectionDuration{0};
@@ -203,10 +202,10 @@ size_t DownstreamConnectionsManager::s_maxCachedConnectionsPerDownstream{10};
 static void decrementTCPClientCount(const ComboAddress& client)
 {
   if (g_maxTCPConnectionsPerClient) {
-    std::lock_guard<std::mutex> lock(s_tcpClientsCountMutex);
-    s_tcpClientsCount.at(client)--;
-    if (s_tcpClientsCount[client] == 0) {
-      s_tcpClientsCount.erase(client);
+    auto tcpClientsCount = s_tcpClientsCount.lock();
+    tcpClientsCount->at(client)--;
+    if (tcpClientsCount->at(client) == 0) {
+      tcpClientsCount->erase(client);
     }
   }
 }
@@ -1282,13 +1281,13 @@ void tcpAcceptorThread(ClientState* cs)
       }
 
       if (g_maxTCPConnectionsPerClient) {
-        std::lock_guard<std::mutex> lock(s_tcpClientsCountMutex);
+        auto tcpClientsCount = s_tcpClientsCount.lock();
 
-        if (s_tcpClientsCount[remote] >= g_maxTCPConnectionsPerClient) {
+        if ((*tcpClientsCount)[remote] >= g_maxTCPConnectionsPerClient) {
           vinfolog("Dropping TCP connection from %s because we have too many from this client already", remote.toStringWithPort());
           continue;
         }
-        s_tcpClientsCount[remote]++;
+        (*tcpClientsCount)[remote]++;
         tcpClientCountIncremented = true;
       }