I have two services communicating via gRPC (A calls B) with a cancellation token of X seconds.
The B service might take longer to process the request than the allowed time. In such cases, I'd like to return a partial result if any and receive it back in service A.
Question: How can I get the returned result back in service A, if possible? If not, not what are the alternatives?
From the documentation, deadline is independently tracked on both sides therefore it cannot work. I am assuming the same is for the CancellationToken.
A C#/pseudocode example. Service A calls B with a time limit of 3 seconds, B will require 10 s to complete and will always be cancelled. I'd like to receive value 100, returned if cancelled in the example case back in service A.
Service A:
public async Task<int> GetNumberFromB()
{
try
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
var result = await _clientB.GetNumber(new GetNumberRequest(), cancellationToken: cts.Token);
return result.Value;
}
catch (RpcException e)
{
// Get result (100) here
}
return 0;
}
Service B:
public async Task<GetNumberReply> GetNumber(GetNumberRequest request, ServerCallContext context)
{
int value = 0;
try
{
// It will always be longer than the 3 seconds as request
await Task.Delay(TimeSpan.FromSeconds(10), context.CancellationToken);
value = 1;
}
catch (OperationCancelledException ex)
{
// Reached after three seconds, when service A calls the
value = 100;
}
return new GetNumberReply { Value = value };
}