From: n0tlu5 Date: Thu, 23 May 2024 01:42:07 +0000 (+0700) Subject: Add selfweighted func desc; reduce to 1 loop X-Git-Tag: dnsdist-2.0.0-alpha1~178^2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f3a65fdc6001aac7277555df861318eee477bd2;p=thirdparty%2Fpdns.git Add selfweighted func desc; reduce to 1 loop --- diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 2259ca561b..cf95e0ea35 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1181,47 +1181,36 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn return pickRandom(items); }); - lua.writeFunction("selfweighted", [](const std::string& url, - const boost::variant& ips, + /* + * Based on the hash of `bestwho`, returns an IP address from the list + * supplied, weighted according to the results of isUp calls. + * @example selfweighted("{ "192.0.2.20", "203.0.113.4", "203.0.113.2" }) + */ + lua.writeFunction("selfweighted", [](const iplist_t& ips, boost::optional options) { - vector > candidates; + vector< pair > items; opts_t opts; if(options) opts = *options; - if(auto simple = boost::get(&ips)) { - vector unit = convIplist(*simple); - candidates.push_back(unit); - } else { - auto units = boost::get(ips); - for(const auto& u : units) { - vector unit = convIplist(u.second); - candidates.push_back(unit); - } - } - for(const auto& unit : candidates) { - vector > conv; - bool available = 0; - for(const auto& c : unit) { - int weight = 0; - weight = g_up.isUp(c, url, opts); - if(weight>0){ - available = 1; - } - conv.emplace_back(weight, c); - } - if(available) { - return pickwhashed(s_lua_record_ctx->bestwho, conv).toString(); + items.reserve(ips.capacity()); + bool available = 0; + + vector conv = convComboAddressList(ips); + for (auto& entry : conv) { + int weight = 0; + weight = g_up.isUp(entry, opts); + if(weight>0){ + available = 1; } + items.emplace_back(weight, entry); } - - // All units down, apply backupSelector on all candidates - vector ret{}; - for(const auto& unit : candidates) { - ret.insert(ret.end(), unit.begin(), unit.end()); + if(available) { + return pickWeightedHashed(s_lua_record_ctx->bestwho, items).toString(); } - return pickrandom(ret).toString(); + // All units down, apply backupSelector on all candidates + return pickWeightedRandom(items).toString(); }); lua.writeFunction("pickrandomsample", [](int n, const iplist_t& ips) {