]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Properly handle built-in LB policies
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 24 Dec 2024 10:57:28 +0000 (11:57 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 16 Jan 2025 08:50:21 +0000 (09:50 +0100)
pdns/dnsdistdist/dnsdist-lbpolicies.cc
pdns/dnsdistdist/dnsdist-lbpolicies.hh
pdns/dnsdistdist/dnsdist-lua-bindings.cc

index 298565cdd6abf8e7825140bc89f5b8be3d9eb111..d5659dae5d7a05bee07516b103f9ce04b3c5e315 100644 (file)
@@ -401,3 +401,18 @@ std::shared_ptr<DownstreamState> ServerPolicy::getSelectedBackend(const ServerPo
 
   return selectedBackend;
 }
+
+namespace dnsdist::lbpolicies
+{
+const std::vector<std::shared_ptr<ServerPolicy>>& getBuiltInPolicies()
+{
+  static const std::vector<std::shared_ptr<ServerPolicy>> s_policies{
+    std::make_shared<ServerPolicy>("firstAvailable", firstAvailable, false),
+    std::make_shared<ServerPolicy>("roundrobin", roundrobin, false),
+    std::make_shared<ServerPolicy>("wrandom", wrandom, false),
+    std::make_shared<ServerPolicy>("whashed", whashed, false),
+    std::make_shared<ServerPolicy>("chashed", chashed, false),
+    std::make_shared<ServerPolicy>("leastOutstanding", leastOutstanding, false)};
+  return s_policies;
+}
+}
index 3ddc4ec592d502867c184239614c2d4506030eee..d975988742ee93bfc380c5789e48c5912a9cf18b 100644 (file)
@@ -106,3 +106,10 @@ std::shared_ptr<DownstreamState> whashedFromHash(const ServerPolicy::NumberedSer
 std::shared_ptr<DownstreamState> chashed(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
 std::shared_ptr<DownstreamState> chashedFromHash(const ServerPolicy::NumberedServerVector& servers, size_t hash);
 std::shared_ptr<DownstreamState> roundrobin(const ServerPolicy::NumberedServerVector& servers, const DNSQuestion* dq);
+
+#include <unordered_map>
+
+namespace dnsdist::lbpolicies
+{
+const std::vector<std::shared_ptr<ServerPolicy>>& getBuiltInPolicies();
+}
index f08746d9f5b2ed3cbf486171d91d30081f7e17a3..8f966b1a124f512ea759a687374e5eb8214d7e37 100644 (file)
@@ -87,14 +87,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
   luaCtx.registerFunction("toString", &ServerPolicy::toString);
   luaCtx.registerFunction("__tostring", &ServerPolicy::toString);
 
-  const std::array<std::shared_ptr<ServerPolicy>, 6> policies = {
-    std::make_shared<ServerPolicy>("firstAvailable", firstAvailable, false),
-    std::make_shared<ServerPolicy>("roundrobin", roundrobin, false),
-    std::make_shared<ServerPolicy>("wrandom", wrandom, false),
-    std::make_shared<ServerPolicy>("whashed", whashed, false),
-    std::make_shared<ServerPolicy>("chashed", chashed, false),
-    std::make_shared<ServerPolicy>("leastOutstanding", leastOutstanding, false)};
-  for (const auto& policy : policies) {
+  for (const auto& policy : dnsdist::lbpolicies::getBuiltInPolicies()) {
     luaCtx.writeVariable(policy->d_name, policy);
   }