- return NULL;
- lock ml(h->cl_mutex);
- if (h->del)
- return NULL;
- if (h->cl)
- return h->cl;
- sockaddr_in dstsock;
- make_sockaddr(h->m.c_str(), &dstsock);
- rpcc *cl = new rpcc(dstsock);
- tprintf("handler_mgr::get_handle trying to bind...%s\n", h->m.c_str());
- int ret;
- // Starting with lab 6, our test script assumes that the failure
- // can be detected by paxos and rsm layer within few seconds. We have
- // to set the timeout with a small value to support the assumption.
- //
- // Note: with RPC_LOSSY=5, your lab would failed to pass the tests of
- // lab 6 and lab 7 because the rpc layer may delay your RPC request,
- // and cause a time out failure. Please make sure RPC_LOSSY is set to 0.
- ret = cl->bind(rpcc::to(1000));
- if (ret < 0) {
- tprintf("handle_mgr::get_handle bind failure! %s %d\n", h->m.c_str(), ret);
- delete cl;
- h->del = true;
- } else {
- tprintf("handle_mgr::get_handle bind succeeded %s\n", h->m.c_str());
- h->cl = cl;
+ return nullptr;
+ lock cl(h->client_mutex);
+ if (!h->valid)
+ return nullptr;
+ if (!h->client) {
+ unique_ptr<rpcc> client(new rpcc(h->destination));
+ LOG("trying to bind..." << h->destination);
+ // The test script assumes that the failure can be detected by paxos and
+ // rsm layer within few seconds. We have to set the timeout with a small
+ // value to support the assumption.
+ //
+ // With RPC_LOSSY=5, tests may fail due to delays and time outs.
+ int ret = client->bind(milliseconds(1000));
+ if (ret < 0) {
+ LOG("bind failure! " << h->destination << " " << ret);
+ h->valid = false;
+ } else {
+ LOG("bind succeeded " << h->destination);
+ h->client = move(client);
+ }