Rohit
Rohit

Reputation: 516

gRPC(Java) - How to check if client is connected to receive server messages in case of "Server Streaming" cases?

I am new to gRPC and trying to understand the concepts. I was going though answer of Mark Roth, and I am still not able to understand why architecture doesn't allow us to check if a stream is open. Let's say, I have a bi-directional rpc like:

StreamObserver<ClientMessage> client = stub.biDirectionalRpc(new StreamObserver<ServerMessage>(){
        @Override
        public void onNext(ServerMessage serverMessage) {
            ...
        }

        @Override
        public void onError(Throwable throwable) {
            client = null; // Connection might already be established again by
                           // the time this statement is reached. (`client` guarded by mutex)
            ...
        }

        @Override
        public void onCompleted() {
            ...
        }
});

Please help me understand how client can check that it is actually able to get messages from server. Relying on 'onError' is out of question because it leads to race conditions while maintaining the sate of this connection.

Upvotes: 0

Views: 888

Answers (1)

Eric Anderson
Eric Anderson

Reputation: 26394

You can learn if the RPC was established by receiving a message on the stream. But there's no way to avoid a race with the RPC failing after it has started.

Even if there was a method to check whether the RPC was working, that information could be out-dated by the time the result is used. if (isRpcWorking()) doSomething(); there is a race between the two methods and a mutex can't prevent the network from failing. The network could even be operational when you send a message, but it fails in the time it takes for the electrons/photons of that message to travel between the client and server. You must accept that failures are racy.

Upvotes: 1

Related Questions