]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Improve the scalability of the MAC address cache 15868/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 18 Jul 2025 10:06:48 +0000 (12:06 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 18 Jul 2025 11:59:15 +0000 (13:59 +0200)
Signed-off-by: Remi Gacogne <remi.gacogne@powerdns.com>
pdns/dnsdistdist/dnsdist-mac-address.cc
pdns/dnsdistdist/dnsdist-mac-address.hh

index 20a7245a9fb33d8c2bca0b7d7469ec325345ef1e..eed10b3f735bea2c42541c1921357730a7691566 100644 (file)
@@ -25,7 +25,7 @@
 
 namespace dnsdist
 {
-LockGuarded<boost::circular_buffer<MacAddressesCache::Entry>> MacAddressesCache::s_cache;
+SharedLockGuarded<boost::circular_buffer<MacAddressesCache::Entry>> MacAddressesCache::s_cache;
 
 int MacAddressesCache::get(const ComboAddress& ca, unsigned char* dest, size_t destLen)
 {
@@ -37,7 +37,7 @@ int MacAddressesCache::get(const ComboAddress& ca, unsigned char* dest, size_t d
   time_t now = time(nullptr);
 
   {
-    auto cache = s_cache.lock();
+    auto cache = s_cache.read_lock();
     for (const auto& entry : *cache) {
       if (entry.ttd >= now && compare(entry.ca, ca) == true) {
         if (!entry.found) {
@@ -51,22 +51,22 @@ int MacAddressesCache::get(const ComboAddress& ca, unsigned char* dest, size_t d
   }
 
   auto res = getMACAddress(ca, reinterpret_cast<char*>(dest), destLen);
+  Entry entry;
+  entry.ca = ca;
+  if (res == 0) {
+    memcpy(entry.mac.data(), dest, entry.mac.size());
+    entry.found = true;
+  }
+  else {
+    memset(entry.mac.data(), 0, entry.mac.size());
+    entry.found = false;
+  }
+  entry.ttd = now + MacAddressesCache::s_cacheValiditySeconds;
   {
-    auto cache = s_cache.lock();
+    auto cache = s_cache.write_lock();
     if (cache->capacity() == 0) {
       cache->set_capacity(MacAddressesCache::s_cacheSize);
     }
-    Entry entry;
-    entry.ca = ca;
-    if (res == 0) {
-      memcpy(entry.mac.data(), dest, entry.mac.size());
-      entry.found = true;
-    }
-    else {
-      memset(entry.mac.data(), 0, entry.mac.size());
-      entry.found = false;
-    }
-    entry.ttd = now + MacAddressesCache::s_cacheValiditySeconds;
     cache->push_back(std::move(entry));
   }
 
index fe2f1f0fe697588ae73b31378b08ceb653ef2608..4fc69156afe1ec6e2bbfef03bf089e8027d916e4 100644 (file)
@@ -45,6 +45,6 @@ private:
 
   static constexpr size_t s_cacheSize{10};
   static constexpr time_t s_cacheValiditySeconds{60};
-  static LockGuarded<boost::circular_buffer<Entry>> s_cache;
+  static SharedLockGuarded<boost::circular_buffer<Entry>> s_cache;
 };
 }