From 831450266acd06f50d3d15119aa3991c37efa7af Mon Sep 17 00:00:00 2001 From: Robin Geuze Date: Sat, 3 Nov 2018 15:08:13 +0100 Subject: [PATCH] Add a PoolAvailableRule to easily add backup pools --- pdns/dnsdist-lua-rules.cc | 6 ++++++ pdns/dnsdistdist/dnsdist-rules.hh | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pdns/dnsdist-lua-rules.cc b/pdns/dnsdist-lua-rules.cc index 28630b1fe..8fc84d494 100644 --- a/pdns/dnsdist-lua-rules.cc +++ b/pdns/dnsdist-lua-rules.cc @@ -440,6 +440,12 @@ void setupLuaRules() return std::shared_ptr(new TimedIPSetRule()); }); + g_lua.writeFunction("PoolAvailableRule", [](std::string poolname) { + setLuaSideEffect(); + auto localPools = g_pools.getCopy(); + return std::shared_ptr(new PoolAvailableRule(localPools, poolname)); + }); + g_lua.registerFunction::*)()>("clear", [](std::shared_ptr tisr) { tisr->clear(); }); diff --git a/pdns/dnsdistdist/dnsdist-rules.hh b/pdns/dnsdistdist/dnsdist-rules.hh index 2321902d9..10a40b601 100644 --- a/pdns/dnsdistdist/dnsdist-rules.hh +++ b/pdns/dnsdistdist/dnsdist-rules.hh @@ -991,3 +991,25 @@ private: boost::optional d_value; std::string d_tag; }; + +class PoolAvailableRule : public DNSRule +{ +public: + PoolAvailableRule(pools_t& pools, const std::string& poolname) : d_poolname(poolname) + { + d_pool = getPool(pools, poolname); + } + + bool matches(const DNSQuestion* dq) const override + { + return (d_pool->countServers(true) > 0); + } + + string toString() const override + { + return "pool '" + d_poolname + "' is available"; + } +private: + std::string d_poolname; + std::shared_ptr d_pool; +}; -- 2.40.0