]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: add fast path to roundrobin load balancing policy 16239/head
authorHolger Hoffstätte <holger@applied-asynchrony.com>
Thu, 9 Oct 2025 20:04:07 +0000 (22:04 +0200)
committerHolger Hoffstätte <holger@applied-asynchrony.com>
Thu, 9 Oct 2025 20:31:02 +0000 (22:31 +0200)
There is no need to collect all servers that are up when the current
server is already a good candidate. This avoids needless heap allocation
and deallocation in the vast majority of cases.

Signed-off-by: Holger Hoffstätte <holger@applied-asynchrony.com>
pdns/dnsdistdist/dnsdist-lbpolicies.cc

index 5bb8648c3380ab68355e6874ee0142c7c91f3d9b..20e6d65afa98c7edfd05176d8c73ba40a9bb8801 100644 (file)
@@ -244,6 +244,14 @@ std::optional<ServerPolicy::SelectedServerPosition> roundrobin(const ServerPolic
     return std::nullopt;
   }
 
+  static unsigned int counter;
+
+  size_t serverIdx = (counter++) % servers.size();
+  auto currentServer = servers.at(serverIdx);
+  if (currentServer.second->isUp()) {
+    return currentServer.first;
+  }
+
   vector<size_t> candidates;
   candidates.reserve(servers.size());
 
@@ -262,8 +270,7 @@ std::optional<ServerPolicy::SelectedServerPosition> roundrobin(const ServerPolic
     }
   }
 
-  static unsigned int counter;
-  return candidates.at((counter++) % candidates.size());
+  return candidates.at(counter % candidates.size());
 }
 
 std::optional<ServerPolicy::SelectedServerPosition> orderedWrandUntag(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dnsQuestion)