From: Christian Herdtweck Date: Fri, 19 Jun 2015 13:22:39 +0000 (+0200) Subject: added clean-up to DnsCache: will remove very old entries after 60 days X-Git-Url: http://developer.intra2net.com/git/?p=pingcheck;a=commitdiff_plain;h=c98b5fcb9695ad7b196f0399adb0fef60e5797d5 added clean-up to DnsCache: will remove very old entries after 60 days --- diff --git a/src/dns/dnscache.cpp b/src/dns/dnscache.cpp index 118f802..f607112 100644 --- a/src/dns/dnscache.cpp +++ b/src/dns/dnscache.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include "dns/dnsmaster.h" @@ -129,6 +130,9 @@ void DnsCache::save_to_cachefile() { try { + // clean up: remove very old entries + remove_old_entries(); + // remember time of save std::string cache_save_time_str = boost::posix_time::to_iso_string( boost::posix_time::second_clock::universal_time() ); @@ -236,6 +240,51 @@ bool DnsCache::check_timestamps(const boost::posix_time::ptime &cache_save_time) } +/** + * @brief remove entries from cache that are older than a certain threshold + * + * this also removes entres from IpCache with no IPs + */ +void DnsCache::remove_old_entries() +{ + boost::posix_time::ptime thresh + = boost::posix_time::second_clock::universal_time() + - boost::gregorian::days( Config::CACHE_REMOVE_OUTDATED_DAYS ); + + // IP cache + bool some_ip_up_to_date; + BOOST_FOREACH( const ip_map_type::value_type key_and_ip, IpCache ) + { + some_ip_up_to_date = false; + BOOST_FOREACH( HostAddress address, key_and_ip.second ) + { + if ( ! address.get_ttl().was_set_before(thresh) ) + { + some_ip_up_to_date = true; + break; + } + } + + if ( ! some_ip_up_to_date ) + { + GlobalLogger.debug() << "DnsCache: Removing empty/outdated IP list " + << "for " << key_and_ip.first.first; + IpCache.erase( key_and_ip.first ); + } + } + + // CNAME cache + BOOST_FOREACH( const cname_map_type::value_type key_and_cname, CnameCache ) + { + if ( key_and_cname.second.Ttl.was_set_before( thresh ) ) + { + GlobalLogger.debug() << "DnsCache: Removing outdated CNAME for " + << key_and_cname.first; + CnameCache.erase( key_and_cname.first ); + } + } +} + // ----------------------------------------------------------------------------- // UPDATE // ----------------------------------------------------------------------------- diff --git a/src/dns/dnscache.h b/src/dns/dnscache.h index b7862a9..83292bc 100644 --- a/src/dns/dnscache.h +++ b/src/dns/dnscache.h @@ -97,6 +97,7 @@ private: const DnsIpProtocol &protocol) const; cname_map_key_type key_for_cname(const std::string &hostname) const; bool check_timestamps(const boost::posix_time::ptime &cache_save_time); + void remove_old_entries(); }; diff --git a/src/dns/timetolive.cpp b/src/dns/timetolive.cpp index 4d628eb..87232df 100644 --- a/src/dns/timetolive.cpp +++ b/src/dns/timetolive.cpp @@ -74,3 +74,8 @@ uint32_t TimeToLive::get_updated_value() const else return original_ttl - elapsed_seconds; } + +bool TimeToLive::was_set_before(const ptime &threshold) const +{ + return TtlSetTime < threshold; +} diff --git a/src/dns/timetolive.h b/src/dns/timetolive.h index e3cd579..088f79e 100644 --- a/src/dns/timetolive.h +++ b/src/dns/timetolive.h @@ -47,6 +47,8 @@ public: uint32_t get_updated_value() const; + bool was_set_before(const boost::posix_time::ptime &threshold) const; + private: /// the numeric time-to-live uint32_t Ttl;