From 495f8e5f1f2c147f7431c5a6bfd4f4606b640fe3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Holger=20Hoffst=C3=A4tte?= Date: Thu, 9 Oct 2025 22:04:07 +0200 Subject: [PATCH] dnsdist: add fast path to roundrobin load balancing policy MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- pdns/dnsdistdist/dnsdist-lbpolicies.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pdns/dnsdistdist/dnsdist-lbpolicies.cc b/pdns/dnsdistdist/dnsdist-lbpolicies.cc index 5bb8648c33..20e6d65afa 100644 --- a/pdns/dnsdistdist/dnsdist-lbpolicies.cc +++ b/pdns/dnsdistdist/dnsdist-lbpolicies.cc @@ -244,6 +244,14 @@ std::optional 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 candidates; candidates.reserve(servers.size()); @@ -262,8 +270,7 @@ std::optional roundrobin(const ServerPolic } } - static unsigned int counter; - return candidates.at((counter++) % candidates.size()); + return candidates.at(counter % candidates.size()); } std::optional orderedWrandUntag(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dnsQuestion) -- 2.47.3