Call cancellation
Another cool thing about gRPC requests is that they can be cancelled. This can be useful to reduce wasted resources after an exception--if the application realizes that it no longer needs the response, it can cancel the RPC. The cancellation is propagated to the server, so the server can see that the client has cancelled and stop processing.
For synchronous APIs, like in Ruby, a cancellation can be a little subtle. You might find yourself more likely using deadlines and deadline propagation (described in the previous section) whereby the RPC is automatically cancelled after a configured duration. For unary RPCs in Ruby, another thread is required to cancel an RPC, since the thread that issued it is blocked, waiting on the response.
In Ruby, the steps to make a cancellable RPC are similar to what we learned for examining
the server’s metadata. Back in the “gRPC Basics” chapter: you get an Operation
from the stub
instead of the actual response:
Ruby...