From: Holger Hoffstätte Date: Thu, 9 Oct 2025 20:04:07 +0000 (+0200) Subject: dnsdist: add fast path to roundrobin load balancing policy X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F16239%2Fhead;p=thirdparty%2Fpdns.git dnsdist: add fast path to roundrobin load balancing policy 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 --- 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)