Discarding an cancelled request

45 views
Skip to first unread message

vicva...@gmail.com

unread,
Jan 25, 2018, 1:04:55 PM1/25/18
to grpc.io

Hi Guys,

I have a grpc service which does some cpu intensive work.
Before starting the work, I want to ensure that the client hasn't cancelled this aforementioned work.
Rather than checking in every service, I would like to write an interceptor which would check and drop the request if it has been cancelled.

This is what I have come up with but I can't figure out how to drop the request and signal to the client.
Any pointers are much appreciated.

Cheers

public final class CancelledRequestInterceptor implements ServerInterceptor{

@Override
public final <R, S> Server.Listener<R> interceptCall( ServerCall<R,S> call, Metadata meta, ServerCallHandler<R, S> next ){

   
boolean isCancelled = Context.current().isCancelled();
   
if( isCancelled ){
       
//How do I discard the call and signal to the client that it has been discarded
   
}else{
       
return next.startCall( call, headers );
   
}

}



Eric Anderson

unread,
Jan 25, 2018, 5:15:36 PM1/25/18
to vicva...@gmail.com, grpc.io
Just handle the ServerCall directly. Since you don't actually need the listener, you can give a no-op one. If you need to provide additional error information, you can provide it in the Metadata.

if (isCancelled) {
  call.close(Status.SOME_STATUS.withDescription("some description"), new Metadata());
  return new ServerCall.Listener<R>() {};
}

--
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 grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
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/44bd8313-88c9-4758-a007-28be38cc4c25%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vicva...@gmail.com

unread,
Jan 26, 2018, 9:27:11 PM1/26/18
to grpc.io
Thanks Eric, I will give it a try.
Reply all
Reply to author
Forward
0 new messages