From: Miod Vallat Date: Wed, 28 May 2025 12:50:31 +0000 (+0200) Subject: Add the ability to purge an entire view from the packetcache. X-Git-Tag: dnsdist-2.0.0-beta1~29^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=493b931199edfd9d284f7d93d22939ef55982d80;p=thirdparty%2Fpdns.git Add the ability to purge an entire view from the packetcache. --- diff --git a/pdns/auth-packetcache.cc b/pdns/auth-packetcache.cc index 16275dd008..2be7017168 100644 --- a/pdns/auth-packetcache.cc +++ b/pdns/auth-packetcache.cc @@ -282,6 +282,24 @@ uint64_t AuthPacketCache::purgeExact(const std::string& view, const DNSName& qna return delcount; } +uint64_t AuthPacketCache::purgeView(const std::string& view) +{ + uint64_t delcount = 0; + + { + auto cache = d_cache.write_lock(); + if (auto iter = cache->find(view); iter != cache->end()) { + auto* map = iter->second.get(); + delcount += purgeLockedCollectionsVector(*map); + cache->erase(iter); + } + } + + *d_statnumentries -= delcount; + + return delcount; +} + /* purges entries from the packetcache. If match ends on a $, it is treated as a suffix */ uint64_t AuthPacketCache::purge(const string &match) { diff --git a/pdns/auth-packetcache.hh b/pdns/auth-packetcache.hh index f5cf00043a..b10a6b210e 100644 --- a/pdns/auth-packetcache.hh +++ b/pdns/auth-packetcache.hh @@ -66,6 +66,7 @@ public: uint64_t purge(const std::string& match); // could be $ terminated. Is not a dnsname! uint64_t purgeExact(const DNSName& qname); // no wildcard matching here uint64_t purgeExact(const std::string& view, const DNSName& qname); // same as above, but in the given view + uint64_t purgeView(const std::string& view); uint64_t size() const { return *d_statnumentries; }; diff --git a/pdns/test-packetcache_cc.cc b/pdns/test-packetcache_cc.cc index 021479e68a..037860130b 100644 --- a/pdns/test-packetcache_cc.cc +++ b/pdns/test-packetcache_cc.cc @@ -708,6 +708,19 @@ BOOST_AUTO_TEST_CASE(test_AuthViews) // Check that requesting from view2 causes a cache miss BOOST_CHECK_EQUAL(queryPacketCache2(PC, ZC, ComboAddress("192.0.2.1"), qname, innerMask, view2, "2.2.2.2"), false); + + // Cache answers for view1 and view2 again + feedPacketCache2(PC, view1, 0x01010101, qname); + feedPacketCache2(PC, view2, 0x02020202, qname); + BOOST_CHECK_EQUAL(PC.size(), 2); + + // Purge view1 + BOOST_CHECK_EQUAL(PC.purgeView(view1), 1); + BOOST_CHECK_EQUAL(PC.size(), 1); + + // Purge view2 + BOOST_CHECK_EQUAL(PC.purgeView(view2), 1); + BOOST_CHECK_EQUAL(PC.size(), 0); } #endif // ] PDNS_AUTH