Various options can be set in the ``options`` parameter:
- - ``selector``: used to pick the IP address from list of viable candidates. Choices include 'pickclosest', 'random', 'hashed'.
+ - ``selector``: used to pick the IP address from list of viable candidates. Choices include 'pickclosest', 'random', 'hashed', 'all', 'none'.
- ``source``: Source IP address to check from
``url``. In addition, multiple groups of IP addresses can be supplied. The
first set with a working (available) IP address is used.
- If all addresses are down, as usual, a random element from all sets is
- returned.
-
:param string url: The url to retrieve.
:param addresses: List of lists of IP addresses to check the URL on.
:param options: Table of options for this specific check, see below.
Various options can be set in the ``options`` parameter:
- - ``selector``: used to pick the IP address from list of viable candidates. Choices include 'pickclosest', 'random', 'hashed'.
+ - ``selector``: used to pick the IP address from list of viable candidates. Choices include 'pickclosest', 'random', 'hashed', 'all', 'none'.
+ - ``defaultSelector``: used to pick the IP address from list of all candidates if all addresses are down. Choices include 'pickclosest', 'random', 'hashed', 'all', 'none'.
- ``source``: Source IP address to check from
- ``stringmatch``: check ``url`` for this string, only declare 'up' if found
return ret;
}
-static ComboAddress useSelector(const boost::optional<std::unordered_map<string, string>>& options, const ComboAddress& bestwho, const vector<ComboAddress>& candidates)
+static std::string getOptionValue(const boost::optional<std::unordered_map<string, string>>& options, const std::string &name, const std::string &defaultValue)
{
- string selector="random";
+ string selector=defaultValue;
if(options) {
- if(options->count("selector"))
- selector=options->find("selector")->second;
+ if(options->count(name))
+ selector=options->find(name)->second;
}
+ return selector;
+}
+
+static vector<ComboAddress> useSelector(const std::string &selector, const ComboAddress& bestwho, const vector<ComboAddress>& candidates)
+{
+ vector<ComboAddress> ret;
- if(selector=="random")
- return pickrandom(candidates);
+ if(selector=="none")
+ return ret;
+ else if(selector=="all")
+ return candidates;
+ else if(selector=="random")
+ ret.emplace_back(pickrandom(candidates));
else if(selector=="pickclosest")
- return pickclosest(bestwho, candidates);
+ ret.emplace_back(pickclosest(bestwho, candidates));
else if(selector=="hashed")
- return hashed(bestwho, candidates);
+ ret.emplace_back(hashed(bestwho, candidates));
+ else {
+ g_log<<Logger::Warning<<"LUA Record called with unknown selector '"<<selector<<"'"<<endl;
+ ret.emplace_back(pickrandom(candidates));
+ }
+
+ return ret;
+}
- g_log<<Logger::Warning<<"LUA Record called with unknown selector '"<<selector<<"'"<<endl;
- return pickrandom(candidates);
+static vector<string> convIpListToString(const vector<ComboAddress> &comboAddresses)
+{
+ vector<string> ret;
+ for (ComboAddress c : comboAddresses)
+ ret.emplace_back(c.toString());
+ return ret;
}
static vector<ComboAddress> convIplist(const iplist_t& src)
// if no IP is available, use selector on the whole set
candidates = std::move(unavailables);
}
- ComboAddress res=useSelector(options, bestwho, candidates);
- return res.toString();
+ vector<ComboAddress> res = useSelector(getOptionValue(options, "selector", "random"), bestwho, candidates);
+ return convIpListToString(res);
});
lua.writeFunction("ifurlup", [&bestwho](const std::string& url,
}
}
if(!available.empty()) {
- ComboAddress res=useSelector(options, bestwho, available);
-
- return res.toString();
+ vector<ComboAddress> res = useSelector(getOptionValue(options, "selector", "random"), bestwho, available);
+ return convIpListToString(res);
}
}
- // All units down, return a single, random record
+ // All units down, apply defaultSelector on all candidates
vector<ComboAddress> ret{};
for(const auto& unit : candidates) {
ret.insert(ret.end(), unit.begin(), unit.end());
}
- return pickrandom(ret).toString();
- });
+ vector<ComboAddress> res = useSelector(getOptionValue(options, "defaultSelector", "random"), bestwho, ret);
+ return convIpListToString(res);
+ });
/* idea: we have policies on vectors of ComboAddresses, like