From: Remi Gacogne Date: Tue, 13 Jan 2026 15:25:10 +0000 (+0100) Subject: dnsdist: Do not keep stale cache entries around for empty pools X-Git-Tag: rec-5.4.0-beta1~42^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F16721%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Do not keep stale cache entries around for empty pools Signed-off-by: Remi Gacogne --- diff --git a/pdns/dnsdistdist/dnsdist-backend.cc b/pdns/dnsdistdist/dnsdist-backend.cc index 112655401c..a755f5be41 100644 --- a/pdns/dnsdistdist/dnsdist-backend.cc +++ b/pdns/dnsdistdist/dnsdist-backend.cc @@ -1152,6 +1152,11 @@ void ServerPool::setECS(bool useECS) updateConsistency(); } +bool ServerPool::shouldKeepStaleData() const +{ + return !getServers().empty() && countServers(true) == 0; +} + namespace dnsdist::backend { void registerNewBackend(std::shared_ptr& backend) diff --git a/pdns/dnsdistdist/dnsdist-server-pool.hh b/pdns/dnsdistdist/dnsdist-server-pool.hh index 78ba591aa9..9aa6d533fe 100644 --- a/pdns/dnsdistdist/dnsdist-server-pool.hh +++ b/pdns/dnsdistdist/dnsdist-server-pool.hh @@ -64,6 +64,7 @@ struct ServerPool size_t poolLoad() const; size_t countServers(bool upOnly) const; bool hasAtLeastOneServerAvailable() const; + bool shouldKeepStaleData() const; const ServerPolicy::NumberedServerVector& getServers() const; void addServer(std::shared_ptr& server); void removeServer(std::shared_ptr& server); diff --git a/pdns/dnsdistdist/dnsdist.cc b/pdns/dnsdistdist/dnsdist.cc index a4bebd84e4..200f4d3d4b 100644 --- a/pdns/dnsdistdist/dnsdist.cc +++ b/pdns/dnsdistdist/dnsdist.cc @@ -2426,7 +2426,7 @@ static void maintThread() has all its backends down) */ if (packetCache->keepStaleData() && !iter->second) { /* so far all pools had at least one backend up */ - if (pool.countServers(true) == 0) { + if (pool.shouldKeepStaleData()) { iter->second = true; } } diff --git a/pdns/dnsdistdist/test-dnsdistserverpool.cc b/pdns/dnsdistdist/test-dnsdistserverpool.cc index f9370adacf..d15a43aa77 100644 --- a/pdns/dnsdistdist/test-dnsdistserverpool.cc +++ b/pdns/dnsdistdist/test-dnsdistserverpool.cc @@ -59,6 +59,7 @@ BOOST_AUTO_TEST_CASE(test_ServerPoolBasics) BOOST_CHECK_EQUAL(pool.countServers(true), 0U); BOOST_CHECK_EQUAL(pool.countServers(false), 0U); BOOST_CHECK_EQUAL(pool.poolLoad(), 0U); + BOOST_CHECK(!pool.shouldKeepStaleData()); { const auto& servers = pool.getServers(); @@ -76,6 +77,7 @@ BOOST_AUTO_TEST_CASE(test_ServerPoolBasics) BOOST_CHECK(!pool.hasAtLeastOneServerAvailable()); BOOST_CHECK_EQUAL(pool.countServers(true), 0U); BOOST_CHECK_EQUAL(pool.countServers(false), 1U); + BOOST_CHECK(pool.shouldKeepStaleData()); BOOST_CHECK_EQUAL(pool.poolLoad(), 0U); { @@ -88,11 +90,13 @@ BOOST_AUTO_TEST_CASE(test_ServerPoolBasics) BOOST_CHECK(pool.hasAtLeastOneServerAvailable()); BOOST_CHECK_EQUAL(pool.countServers(true), 1U); BOOST_CHECK_EQUAL(pool.countServers(false), 1U); + BOOST_CHECK(!pool.shouldKeepStaleData()); /* now remove it */ pool.removeServer(ds); BOOST_CHECK_EQUAL(pool.countServers(true), 0U); BOOST_CHECK_EQUAL(pool.countServers(false), 0U); + BOOST_CHECK(!pool.shouldKeepStaleData()); { const auto& servers = pool.getServers(); BOOST_CHECK(servers.empty());