From: Remi Gacogne Date: Fri, 30 Apr 2021 13:48:58 +0000 (+0200) Subject: dnsdist: Convert the TCP client counts map to LockGuarded X-Git-Tag: dnsdist-1.7.0-alpha1~62^2~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d313832e1363aaacb7db1546fcdbd9f7ffd148e;p=thirdparty%2Fpdns.git dnsdist: Convert the TCP client counts map to LockGuarded --- diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index cf253caf7c..0a7da95372 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -55,8 +55,7 @@ Let's start naively. */ -static std::mutex s_tcpClientsCountMutex; -static std::map s_tcpClientsCount; +static LockGuarded> 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 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 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; }