From: Robin Geuze Date: Sat, 3 Nov 2018 14:08:13 +0000 (+0100) Subject: Add a PoolAvailableRule to easily add backup pools X-Git-Tag: dnsdist-1.3.3~2^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=831450266acd06f50d3d15119aa3991c37efa7af;p=thirdparty%2Fpdns.git Add a PoolAvailableRule to easily add backup pools --- diff --git a/pdns/dnsdist-lua-rules.cc b/pdns/dnsdist-lua-rules.cc index 28630b1fe7..8fc84d4940 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 2321902d92..10a40b6014 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; +};