in src/host_selector/round_robin_host_selector.cc [194:241]
void RoundRobinHostSelector::update_props_host_weight(
const std::shared_ptr<round_robin_property::RoundRobinClusterInfo>& info,
const std::unordered_map<std::string, std::string>& props) {
if (auto itr = props.find(round_robin_property::HOST_WEIGHT_KEY); itr != props.end()) {
std::string host_weigths_str = itr->second;
if (host_weigths_str.empty()) {
info->cluster_weight_map.clear();
info->last_host_weight_str = "";
return;
}
std::istringstream stream(host_weigths_str);
std::string token;
std::vector<std::string> host_weight_groups;
while (std::getline(stream, token, ',')) {
host_weight_groups.push_back(token);
}
for (const std::string& host_weight_pair : host_weight_groups) {
std::istringstream stream(host_weight_pair);
std::string token;
std::vector<std::string> host_weight_split;
while (std::getline(stream, token, ':')) {
host_weight_split.push_back(token);
}
if (host_weight_split.size() != 2) {
throw std::runtime_error("Invalid format for host weight pair.");
}
std::string host_name = host_weight_split.at(0);
std::string host_weight = host_weight_split.at(1);
if (host_name.empty() || host_weight.empty()) {
throw std::runtime_error("Host Name and/or host weight missing.");
}
try {
int set_weight = convert_to_int(host_weight);
if (set_weight < DEFAULT_WEIGHT) {
throw std::runtime_error("Invalid host weight.");
}
info->cluster_weight_map[host_name] = set_weight;
info->last_host_weight_str = host_weigths_str;
} catch (const std::exception& e) {
throw std::runtime_error("Host weight not parsable as an integer.");
}
}
}
}