]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Merge pull request #7140 from RobinGeuze/addPoolAvailableRule
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Tue, 6 Nov 2018 22:48:26 +0000 (23:48 +0100)
committerGitHub <noreply@github.com>
Tue, 6 Nov 2018 22:48:26 +0000 (23:48 +0100)
Add a PoolAvailableRule to easily add backup pools

pdns/dnsdist-lua-rules.cc
pdns/dnsdistdist/dnsdist-rules.hh
pdns/dnsdistdist/docs/rules-actions.rst

index 28630b1fe7c15be9f5a2281e98d1302dccf6cffc..c209d10a500822b1af30299a89b5b9df4ebc67ea 100644 (file)
@@ -440,6 +440,10 @@ void setupLuaRules()
       return std::shared_ptr<TimedIPSetRule>(new TimedIPSetRule());
     });
 
+  g_lua.writeFunction("PoolAvailableRule", [](std::string poolname) {
+    return std::shared_ptr<DNSRule>(new PoolAvailableRule(poolname));
+  });
+
   g_lua.registerFunction<void(std::shared_ptr<TimedIPSetRule>::*)()>("clear", [](std::shared_ptr<TimedIPSetRule> tisr) {
       tisr->clear();
     });
index 2321902d92ca3591cdd66fb1ee6c1712a721d8cd..f343f4f5db82b7356553aa5baf9a50a991184c8d 100644 (file)
@@ -991,3 +991,24 @@ private:
   boost::optional<std::string> d_value;
   std::string d_tag;
 };
+
+class PoolAvailableRule : public DNSRule
+{
+public:
+  PoolAvailableRule(const std::string& poolname) : d_pools(&g_pools), d_poolname(poolname)
+  {
+  }
+
+  bool matches(const DNSQuestion* dq) const override
+  {
+    return (getPool(*d_pools, d_poolname)->countServers(true) > 0);
+  }
+
+  string toString() const override
+  {
+    return "pool '" + d_poolname + "' is available";
+  }
+private:
+  mutable LocalStateHolder<pools_t> d_pools;
+  std::string d_poolname;
+};
index 801879fed15394893705accb4905e572b825d50d..7838f6dd4e8929af14afc1232641671a70a0937e 100644 (file)
@@ -512,6 +512,16 @@ Functions for manipulating Self-Answered Response Rules:
 
   Move the last self answered response rule to the first position.
 
+Function for pool related rules
+
+.. function:: PoolAvailableRule(poolname)
+
+  .. versionadded:: 1.3.3
+
+  Check whether a pool has any servers available to handle queries
+
+  :param string poolname: Pool to check
+
 .. _RulesIntro:
 
 Matching Packets (Selectors)