Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can gRPC server reclaim the DEADLINE_EXCEEDED thread immediately?

Is it right to say that - java gRPC server thread will still run even after the DEADLINE time. But, gRPC server will stop/block that thread only from making any subsequent gRPC calls since the DEADLINE time has crossed?

If the above is a correct statement, then is there a way to stop / block the thread making any Redis / DB calls as well for which DEADLINE time has crossed ? Or once the DEADLINE time is crossed, interrupt the thread immedietly?

like image 620
Venkata Naresh Avatar asked Oct 17 '25 02:10

Venkata Naresh


1 Answers

Is it right to say that - java gRPC server thread will still run even after the DEADLINE time.

Correct. Java doesn't offer any real alternatives.

But, gRPC server will stop/block that thread only from making any subsequent gRPC calls since the DEADLINE time has crossed?

Mostly. Outgoing gRPC calls observe the io.grpc.Context, which means deadlines and cancellations are propagated (unless you fail to propagate Context to another thread or use Context.fork()).

If the above is a correct statement, then is there a way to stop / block the thread making any Redis / DB calls as well for which DEADLINE time has crossed ? Or once the DEADLINE time is crossed, interrupt the thread immedietly?

You can listen for the Context cancellation via Context.addListener(). The gRPC server will cancel the Context when the deadline expires and if the client cancels the RPC. This notification is how outgoing RPCs are cancelled.

I will note that thread interruption is a bit involved to perform without racing. If you want interruption and don't have a Future already, I suggest wrapping your work in a FutureTask (and simply calling FutureTask.run() on the current thread) in order to get its non-racy cancel(true) implementation.

final FutureTask<Void> future = new FutureTask<Void>(work, null);
Context current = Context.current();
CancellationListener listener = new CancellationListener() {
  @Override public void cancelled(Context context) {
    future.cancel(true);
  }
};
current.addListener(listener);
future.run();
current.removeListener(listener);
like image 101
Eric Anderson Avatar answered Oct 19 '25 16:10

Eric Anderson