From: Kees Monshouwer Date: Thu, 12 Mar 2020 18:17:19 +0000 (+0100) Subject: auth: make sure we look at 10% of all cached items during cleanup X-Git-Tag: dnsdist-1.5.0-alpha1~16^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09d5ac4829c64e672e597802ae551899b82aed9a;p=thirdparty%2Fpdns.git auth: make sure we look at 10% of all cached items during cleanup --- diff --git a/pdns/auth-packetcache.cc b/pdns/auth-packetcache.cc index fd96a16257..71dbf134d5 100644 --- a/pdns/auth-packetcache.cc +++ b/pdns/auth-packetcache.cc @@ -248,11 +248,7 @@ uint64_t AuthPacketCache::purge(const string &match) void AuthPacketCache::cleanup() { - uint64_t maxCached = d_maxEntries; - uint64_t cacheSize = *d_statnumentries; - uint64_t totErased = 0; - - totErased = pruneLockedCollectionsVector(d_maps, maxCached, cacheSize); + uint64_t totErased = pruneLockedCollectionsVector(d_maps); *d_statnumentries -= totErased; DLOG(g_log<<"Done with cache clean, cacheSize: "<<(*d_statnumentries)<<", totErased"<(d_maps, maxCached, cacheSize); - + uint64_t totErased = pruneLockedCollectionsVector(d_maps); *d_statnumentries -= totErased; + DLOG(g_log<<"Done with cache clean, cacheSize: "<<*d_statnumentries<<", totErased"< void moveCacheItemToBack(T& collection, typena moveCacheItemToFrontOrBack(collection, iter, false); } -template uint64_t pruneLockedCollectionsVector(vector& maps, uint64_t maxCached, uint64_t cacheSize) +template uint64_t pruneLockedCollectionsVector(vector& maps) { - time_t now = time(nullptr); uint64_t totErased = 0; - uint64_t toTrim = 0; - uint64_t lookAt = 0; - - // two modes - if toTrim is 0, just look through 10% of the cache and nuke everything that is expired - // otherwise, scan first 5*toTrim records, and stop once we've nuked enough - if (maxCached && cacheSize > maxCached) { - toTrim = cacheSize - maxCached; - lookAt = 5 * toTrim; - } else { - lookAt = cacheSize / 10; - } + time_t now = time(nullptr); for(auto& mc : maps) { WriteLock wl(&mc.d_mut); + + uint64_t lookAt = (mc.d_map.size() + 9) / 10; // Look at 10% of this shard + uint64_t erased = 0; + auto& sidx = boost::multi_index::get(mc.d_map); - uint64_t erased = 0, lookedAt = 0; - for(auto i = sidx.begin(); i != sidx.end(); lookedAt++) { - if (i->ttd < now) { + for(auto i = sidx.begin(); i != sidx.end() && lookAt > 0; lookAt--) { + if(i->ttd < now) { i = sidx.erase(i); erased++; } else { ++i; } - - if(toTrim && erased > toTrim / maps.size()) - break; - - if(lookedAt > lookAt / maps.size()) - break; } totErased += erased; }