added clean-up to DnsCache: will remove very old entries after 60 days
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 19 Jun 2015 13:22:39 +0000 (15:22 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 19 Jun 2015 13:22:39 +0000 (15:22 +0200)
src/dns/dnscache.cpp
src/dns/dnscache.h
src/dns/timetolive.cpp
src/dns/timetolive.h

index 118f802..f607112 100644 (file)
@@ -35,6 +35,7 @@
 #include <boost/serialization/vector.hpp>
 #include <boost/archive/xml_oarchive.hpp>
 #include <boost/archive/xml_iarchive.hpp>
+#include <boost/date_time/gregorian/gregorian_types.hpp>
 
 #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
 // -----------------------------------------------------------------------------
index b7862a9..83292bc 100644 (file)
@@ -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();
 
 };
 
index 4d628eb..87232df 100644 (file)
@@ -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;
+}
index e3cd579..088f79e 100644 (file)
@@ -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;