From: n0tlu5 Date: Thu, 19 Dec 2024 14:15:56 +0000 (+0700) Subject: rename selfweighted to pickselfweighted; add pickselfweighted docs X-Git-Tag: dnsdist-2.0.0-alpha1~178^2~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1d2feead3fca1ae8a23501fb893eba36d94fb1be;p=thirdparty%2Fpdns.git rename selfweighted to pickselfweighted; add pickselfweighted docs --- diff --git a/docs/lua-records/functions.rst b/docs/lua-records/functions.rst index 45af5dde9a..715980e120 100644 --- a/docs/lua-records/functions.rst +++ b/docs/lua-records/functions.rst @@ -128,6 +128,21 @@ Record creation functions This function also works for CNAME or TXT records. +.. function:: pickselfweighted(addresses[, options]) + + Selects an IP address from the supplied list, weighted according to the results of `isUp` checks. Each address is evaluated, and if its associated weight (from `isUp`) is greater than 0, it is considered for selection using a weighted hash based on `bestwho`. If no address is "up," the function defaults to a random selection. + + :param addresses: A list of IP addresses to evaluate. + :param options: (Optional) A table of options for this specific check. Supports: + - ``source``: Source address for the check. + - ``timeout``: Maximum time in seconds for the check (default 2). + + Example usage:: + + pickselfweighted({ "192.0.2.20", "203.0.113.4", "203.0.113.2" }, { source = "192.0.2.1", timeout = 1 }) + + This function is ideal for scenarios where weighted selection is necessary to prioritize "up" addresses while still offering fallback behavior when all addresses are down. + .. function:: pickrandomsample(number, values) Returns N random values from the list supplied. diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 0a0f5dbad0..fc0314ece5 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1180,9 +1180,9 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn /* * 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" }) + * @example pickselfweighted("{ "192.0.2.20", "203.0.113.4", "203.0.113.2" }) */ - lua.writeFunction("selfweighted", [](const iplist_t& ips, + lua.writeFunction("pickselfweighted", [](const iplist_t& ips, boost::optional options) { vector< pair > items; opts_t opts;