This is a copy of my question on SO:
https://stackoverflow.com/questions/54588382/how-can-a-grpc-server-notice-that-the-client-has-cancelled-a-server-side-streami
I want to use gRPC (in Java) to let clients subscribe to events generated
by the server. I have an RPC declared like so:
rpc Subscribe (SubscribeRequest) returns (stream SubscribeResponse);
where the returned stream is infinite. To "unsubscribe", clients cancel the
RPC (btw. is there a cleaner way?).
I have figured out how the client can cancel the call:
Context.CancellableContext cancellableContext =
Context.current().withCancellation();
cancellableContext.run(() -> {
stub.subscribe(request, callback);
});
// do other stuff / wait for reason to unsubscribe
cancellableContext.cancel(new InterruptedException());
However, the server does not seem to notice that a client has cancelled its
call. I'm testing this with a dummy server implementation:
@Override
public void subscribe(SubscribeRequest request,
StreamObserver<SubscribeResponse> responseObserver) {
// in real code, this will happen in a separate thread.
while (!Thread.interrupted()) {
responseObserver.onNext(SubscribeResponse.getDefaultInstance());
}
}
The server will happily continue sending its messages into the ether. How
can the server recognize that the call was cancelled by the client and thus
stop sending responses?
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/9930adf4-5a54-4582-907a-98dde7f46577%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.