From: Kees Monshouwer Date: Mon, 26 Sep 2016 19:04:24 +0000 (+0200) Subject: cleanup packetcache cleanup() X-Git-Tag: dnsdist-1.1.0-beta2~108^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2f03ff47fae530dccac9c531457248424fd0b4cb;p=pdns cleanup packetcache cleanup() --- diff --git a/pdns/packetcache.cc b/pdns/packetcache.cc index 498c92a53..3b0d301d0 100644 --- a/pdns/packetcache.cc +++ b/pdns/packetcache.cc @@ -393,52 +393,44 @@ map PacketCache::getCounts() void PacketCache::cleanup() { - unsigned int maxCached=::arg().asNum("max-cache-entries"); - unsigned int toTrim=0; - - unsigned long cacheSize=*d_statnumentries; - + unsigned int maxCached = ::arg().asNum("max-cache-entries"); + unsigned long cacheSize = *d_statnumentries; + + // 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 + unsigned int toTrim = 0, lookAt = 0; if(maxCached && cacheSize > maxCached) { toTrim = cacheSize - maxCached; + lookAt = 5 * toTrim; + } else { + lookAt = cacheSize / 10; } - unsigned int 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(toTrim) - lookAt=5*toTrim; - else - lookAt=cacheSize/10; + DLOG(L<<"Starting cache clean, cacheSize: "<(mc.d_map); - unsigned int erased=0, lookedAt=0; - for(auto i=sidx.begin(); i != sidx.end(); lookedAt++) { + unsigned int erased = 0, lookedAt = 0; + for(auto i = sidx.begin(); i != sidx.end(); lookedAt++) { if(i->ttd < now) { - sidx.erase(i++); - erased++; - } - else { - ++i; + sidx.erase(i); + erased++; } if(toTrim && erased > toTrim / d_maps.size()) - break; - + break; + if(lookedAt > lookAt / d_maps.size()) - break; + break; + + i++; } totErased += erased; } - // if(totErased) - // cerr<<"erased: "<