How to tell if connecting to the server fails? (C++)

3,483 views
Skip to first unread message

thej...@gmail.com

unread,
Sep 15, 2015, 10:33:28 PM9/15/15
to grpc.io
How do I detect that I can't connect to the specified server in C++? As far as I can tell, the attempt to connect currently just hangs forever. Is there any way to return programmatically on failure to connect to the server?

Florian Schott

unread,
Sep 18, 2015, 1:39:52 AM9/18/15
to grpc.io, thej...@gmail.com
Hi,
I got the same problem, my current solution ist to set a deadline to my rpc calls and if this exceeds, I assume the connection is down.
This can be achieved relatively easy: the context object has a method set_deadline which can take a  timespec.
If the RPC Call times out, you'll get the grpc status DEADLINE_EXCEEDED.

Example with five seconds timeout:

    ClientContext context;

    gpr_timespec timeOut;

    timeOut.tv_sec = 5;
    timeOut.tv_nsec = 0;
    timeOut.clock_type = GPR_TIMESPAN;
    context.set_deadline(timeOut);

    grpc::Status retStat = serviceStub->myRPCCall(&context, request, &response);

    if (!retStat.ok() && retStat.error_code() == DEADLINE_EXCEEDED)
    {
        // call timed out

Yang Gao

unread,
Sep 18, 2015, 4:44:43 AM9/18/15
to Florian Schott, grpc.io, Alex Golec
You may want to check out the channel connectivity API in channel.h. The state definition is at https://github.com/grpc/grpc/blob/master/doc/connectivity-semantics-and-api.md.

A WaitUntilReady() may look like this:

bool WaitUntilReady(channel, deadline) {
  auto state = channel->GetState(true);
  while (state != GRPC_CHANNEL_READY) {
    if (!WaitForStateChange(state, deadline)) { return false; }
    state = channel->GetState(true);
  }
  return true;
}

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/b2f79d58-9007-4fff-b591-5653f2a9bd48%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Florian Schott

unread,
Sep 18, 2015, 4:50:28 AM9/18/15
to grpc.io, sequ...@googlemail.com, thej...@gmail.com
Hi,

yes this would be a nice solution, i also tried this, but in my case I didn't get this to work. I only get the state GRPC_CHANNEL_IDLE, regardless if it is conencted or not (in fact even when there is no server present at all).

Yang Gao

unread,
Sep 18, 2015, 4:56:01 AM9/18/15
to Florian Schott, grpc.io, Alex Golec
Did you call GetState(true) ? Passing in true will make the channel start connecting.

Florian Schott

unread,
Sep 18, 2015, 5:00:01 AM9/18/15
to grpc.io, sequ...@googlemail.com, thej...@gmail.com
yes I did..

just tried it again, because something like this would be really nice to have for me. I do channel->GetSteate(true) in  a loop with a Sleep and while waching it right now the state does not change. Even doesn't matter if I start/shut down the server in the background...
BTW I'm using 0.11.0 right now.

However, I worked around with the deadline-check I mentioned above. But if this solution (ChannelState) would work, it qould be very nice to set up a background-monitoring-thread......

Yang Gao

unread,
Sep 18, 2015, 1:44:04 PM9/18/15
to Florian Schott, grpc.io, Alex Golec
It should not stay in IDLE no matter there is a server or not. If you have a (non)working example code snippet, I can help look at it.

Florian Schott

unread,
Sep 23, 2015, 2:53:17 AM9/23/15
to grpc.io, sequ...@googlemail.com, thej...@gmail.com
Hi,

it is as simple as this:


    std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(os.str(), grpc::InsecureCredentials());
    _serviceStub = dataService::NewStub(channel);

    auto state = channel->GetState(true);

os is a ostringstream wcontaining "localhost:21000"
State is GRPC_CHANNEL_IDLE, no matter if server is up or down. Also later in the program, if i check the status, it's always GRPC_CHANNEL_IDLE..
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages