in mcrouter/ServiceInfo-inl.h [461:510]
void ServiceInfo<RouterInfo>::ServiceInfoImpl::handleRequest(
folly::StringPiece key,
const std::shared_ptr<
ProxyRequestContextTyped<RouterInfo, ServiceInfoRequest>>& ctx) const {
auto p = key.find('(');
auto cmd = key;
folly::StringPiece argsStr(key.end(), key.end());
if (p != folly::StringPiece::npos && key.back() == ')') {
assert(key.size() - p >= 2);
cmd = folly::StringPiece(key.begin(), key.begin() + p);
argsStr =
folly::StringPiece(key.begin() + p + 1, key.begin() + key.size() - 1);
}
std::vector<folly::StringPiece> args;
if (!argsStr.empty()) {
// Handle keys with commas correctly for route and route_handles subcommands
if (cmd.startsWith("route")) {
const auto pos = argsStr.find(',');
if (pos != std::string::npos) {
args.emplace_back(argsStr.data(), pos);
args.emplace_back(argsStr.data() + pos + 1, argsStr.size() - pos - 1);
}
} else {
folly::split(',', argsStr, args);
}
}
std::string replyStr;
try {
if (cmd == "route") {
/* Route is a special case since it involves background requests */
handleRouteCommand(ctx, args);
return;
}
auto it = commands_.find(cmd.str());
if (it == commands_.end()) {
throw std::runtime_error("unknown command: " + cmd.str());
}
replyStr = it->second(args);
if (!replyStr.empty() && replyStr.back() == '\n') {
replyStr = replyStr.substr(0, replyStr.size() - 1);
}
} catch (const std::exception& e) {
replyStr = std::string("ERROR: ") + e.what();
}
ReplyT<ServiceInfoRequest> reply(carbon::Result::FOUND);
reply.value_ref() = folly::IOBuf(folly::IOBuf::COPY_BUFFER, replyStr);
ctx->sendReply(std::move(reply));
}