I'm trying to understand how many streams are created in a single grpc connection whenever I send a request using StreamObserver object in a bidirectional streaming using grpc-java Below is my code context
StreamObserver<Response> responseStreamObserver1 = getResponseObserver();
StreamObserver<Response> responseStreamObserver2 = getResponseObserver();
StreamObserver<Request> streamClientSender1 = stub.sendRequest(responseStreamObserver1);
StreamObserver<Request> streamClientSender2 = stub.sendRequest(responseStreamObserver2);
My questions are:
I tried printing the objects on the server and see
Seems like above two conditions were true. But just wanted to verify if it applies to streams also. in other words, A stream is mapped to an sender object(in above case streamClientSender2 and streamClientSender1) and it can be reused by reusing an object to send or receiving something.
An HTTP/2 stream is created for each RPC. So stub.sendRequest(req)
creates the stream and that stream lives for the life of the RPC. (Things are a more complicated if retries are in use, but this is still a fine mental model.)
onNext()
simply sends a message on the existing streamstub.sendRequest()
twice creates two streamsOn server-side, the responseObserver
instance doesn't influence how many streams are created. There is simply one stream per RPC. If you choose to use the same responseObserver for two RPCs, then you have two streams that each notify the same object for incoming messages. Sharing StreamObservers across RPCs is an API violation of StreamObserver, because onCompleted()
will be called multiple times and the calls to the observer will not be synchronized.