Cancel gRPC streaming call


What's inside this article ⌄
  • Cancelling gRPC server-streaming call from client
  • GRPC streaming cancel
  • Cancel server steaming gRPC
  • GRPC cancel call from client

When using gRPC server-streaming call, you may need to interrupt the execution of the request on the server from the client side.

We have a service on the server YourServiceImpl.java:

public class YourServiceImpl extends YourService.YourServiceImplBase {
  @Override 
  public void yourMethod(ProtoRequest request, StreamObserver<ProtoResponse> reponseObserver) {
  	while (/*...*/) {
      Object result = heavyCompute();
      reponseObserver.onNext(result);
  	}
    reponseObserver.onCompleted();
  }
}

And the sendRequest() method on the client Client.java:

public void sendRequest(ProtoRequest request) {
  ManagedChannel channel = buildChannel();
  YourService.YourServiceBlockingStub stub = YourService.newBlockingStub(channel);
  stub.yourMethod(request);
}

Suppose, that at some moment we need to send a message from the client to the server that the request has been interrupted, and the while loop on the server has stopped. This can be done using the Context class.

Let’s add the stopRequest() method to the client code for request interrupting to Client.java:

public void stopRequest() {
    Context.current().withCancellation().cancel(new InterruptedException());
}

You can specify any exception as the parameter for the cancel().

Next, add a condition to the body of the while loop on the server YourServiceImpl.java:

public class YourServiceImpl extends YourService.YourServiceImplBase {
  @Override 
  public void yourMethod(ProtoRequest request, StreamObserver<ProtoResponse> reponseObserver) {
    while (/*...*/) {
      Object result = heavyCompute();
      reponseObserver.onNext(result);
      if (Context.current().isCancelled()) {
        return;
      }
    }
    reponseObserver.onCompleted();
  }
}