Found a solution: https://stackoverflow.com/questions/54588382/how-can-a-grpc-server-notice-that-the-client-has-cancelled-a-server-side-streami/54589509#54589509
On Friday, February 8, 2019 at 10:09:28 AM UTC+1, [email protected] wrote: > > 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/1a602864-ddf6-473d-b638-80f910a5ba78%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
