Tinyden
Tinyden

Reputation: 574

How to tell gRPC client tells whether gRPC server has cancelled?

I'm using a synchronous bidirectional streaming grpc service, and I've let Server-side to close connection when timeout detected.

// server-side code
if (timeout-detected) {
  server_context_->TryCancel();
}

My question is: how do I detect whether the connection still valid on client-side? If so, I could reestablish connection. I've tryed

// client-side code
if (client_reader_writer_->Write(&request)) {
  // I consider it connection is still valid
} else {
  // connection decided cancelled. Re-establish connection and do something.
}

But client_reader_writer_->Write(&request) return true, even after the server's log has shown cancelled.

If someone could give me some hints on this, I would be much grateful!

Upvotes: 1

Views: 1184

Answers (1)

Karthik Balaguru
Karthik Balaguru

Reputation: 7852

If your concern is about link, Keepalive can be used a mechanism to check if the channel is fine by transmitting HTTP2 pings at regular intervals and if there is no response, it is considered as broken.

Other approach is you can also come up with your own "heartbeat" mechanism where you can have the heartbeat sent periodically to check on server/network failure while trying to write to socket.

For server timeout in a typical scenario, indication to client can be done using context.abort(grpc.StatusCode.DEADLINE_EXCEEDED, 'RPC Time Out!')

Here is a reference for the same.

Upvotes: 1

Related Questions