in mcrouter/lib/carbon/example/ThreadAffinityHelloGoodbye.cpp [155:225]
int main(int argc, char** argv) {
folly::init(&argc, &argv);
// Start a single AsyncMcServer instance
XLOG(INFO, "Starting AsyncMcServer");
facebook::memcache::AsyncMcServer server(getOpts());
spawnServer(server);
// Create CarbonRouterInstance
XLOG(INFO, "Creating CarbonRouterInstance");
McrouterOptions routerOpts;
routerOpts.num_proxies = FLAGS_num_proxies;
routerOpts.asynclog_disable = true;
routerOpts.thread_affinity = FLAGS_thread_affinity;
// Make port configurable
routerOpts.config_str = R"(
{
"pools": {
"A": {
"servers": [ "127.0.0.1:11303" ],
"protocol": "caret"
}
},
"route": {
"type": "DuplicateRoute",
"target": "PoolRoute|A",
"copies": 5
}
}
)";
auto router = CarbonRouterInstance<HelloGoodbyeRouterInfo>::init(
"threadAffinityRouter", routerOpts);
if (!router) {
XLOG(ERR) << "Failed to initialize router!";
return 0;
}
SCOPE_EXIT {
// Release all router resources on exit
router->shutdown();
server.shutdown();
server.join();
freeAllRouters();
};
XLOGF(INFO, "Creating {} CarbonRouterClient", FLAGS_num_clients);
// Create CarbonRouterClient's
std::vector<Pointer> clients;
for (int i = 0; i < FLAGS_num_clients; ++i) {
clients.push_back(
router->createClient(0 /* max_outstanding_requests */, false));
}
for (int i = 0; i < FLAGS_num_clients; ++i) {
XLOGF(
INFO,
"Sending {} requests to CarbonRouterClient {}",
FLAGS_num_req_per_client,
i);
for (int j = 0; j < FLAGS_num_req_per_client; ++j) {
sendHelloRequestSync(clients[i].get(), folly::sformat("key:{}{}", i, j));
}
}
XLOGF(
INFO,
"Total Connections: {}. Current Connections: {}.",
gTotalConns.load(),
gCurrentConns.load());
return 0;
}