From: Remi Gacogne Date: Fri, 7 Mar 2025 11:34:46 +0000 (+0100) Subject: dnsdist: Clear owned downstream TCP connections as soon as possible X-Git-Tag: dnsdist-2.0.0-alpha1~6^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bcf887845bea6ac273b885da694ee70e80569824;p=thirdparty%2Fpdns.git dnsdist: Clear owned downstream TCP connections as soon as possible --- diff --git a/pdns/dnsdistdist/dnsdist-tcp-upstream.hh b/pdns/dnsdistdist/dnsdist-tcp-upstream.hh index 4e66bd58bf..984f5d03ad 100644 --- a/pdns/dnsdistdist/dnsdist-tcp-upstream.hh +++ b/pdns/dnsdistdist/dnsdist-tcp-upstream.hh @@ -117,6 +117,7 @@ public: std::shared_ptr getDownstreamConnection(std::shared_ptr& backend, const std::unique_ptr>& tlvs, const struct timeval& now); void registerOwnedDownstreamConnection(std::shared_ptr& conn); + void clearOwnedDownstreamConnections(const std::shared_ptr& downstream); static size_t clearAllDownstreamConnections(); diff --git a/pdns/dnsdistdist/dnsdist-tcp.cc b/pdns/dnsdistdist/dnsdist-tcp.cc index db61d8817f..8f8b810330 100644 --- a/pdns/dnsdistdist/dnsdist-tcp.cc +++ b/pdns/dnsdistdist/dnsdist-tcp.cc @@ -113,6 +113,7 @@ static std::pair, bool> getOwnedDownstre if (conn->matchesTLVs(tlvs)) { DEBUGLOG("Got one owned connection accepting more for " << backend->getName()); conn->setReused(); + ++backend->tcpReusedConnections; return {conn, tlvsMismatch}; } DEBUGLOG("Found one connection to " << backend->getName() << " but with different TLV values"); @@ -129,6 +130,10 @@ std::shared_ptr IncomingTCPConnectionState::getDownstrea auto [downstream, tlvsMismatch] = getOwnedDownstreamConnection(d_ownedConnectionsToBackend, backend, tlvs); if (!downstream) { + if (backend->d_config.useProxyProtocol && tlvsMismatch) { + clearOwnedDownstreamConnections(backend); + } + /* we don't have a connection to this backend owned yet, let's get one (it might not be a fresh one, though) */ downstream = t_downstreamTCPConnectionsManager.getConnectionToDownstream(d_threadData.mplexer, backend, now, std::string()); // if we had an existing connection but the TLVs are different, they are likely unique per query so do not bother keeping the connection @@ -309,6 +314,11 @@ void IncomingTCPConnectionState::registerOwnedDownstreamConnection(std::shared_p } } +void IncomingTCPConnectionState::clearOwnedDownstreamConnections(const std::shared_ptr& downstream) +{ + d_ownedConnectionsToBackend.erase(downstream); +} + /* called when the buffer has been set and the rules have been processed, and only from handleIO (sometimes indirectly via handleQuery) */ IOState IncomingTCPConnectionState::sendResponse(const struct timeval& now, TCPResponse&& response) {