I have this implementation for subscribing the GRPC stream services, but, I have to identify when one of the services goes off and calls the event to notify the UI.
public async Task Subscribe()
{
await Policy
.Handle<RpcException>(e => e.Status.StatusCode == StatusCode.Unavailable)
.WaitAndRetryAsync(
10,
attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)),
onRetry: (e, ts) => {
logger.Warning("Subscription connection lost. Trying to reconnect in {Seconds}s!", ts.Seconds);
})
.ExecuteAsync(() => {
IAsyncEnumerable<Notification> stream = await subscribe.Subscribe(currentUser)
await foreach (Notification? ev in stream)
{
switch (reply.ActionCase)
{
case Notification.ActionOneofCase.Service1:
logger.Warning("Incoming reply 'Service1'");
break;
case Notification.ActionOneofCase.Service2:
//TODO:
break;
}
}
});
}
I tried to use polly, but I don't know how to get when one specific service is down. I need to identify when one of the services is off to notify the UI. What would be the best approach to identity which service goes off?
EDIT:
That's how each service is injected:
private static void AddGrpcService<T>(IServiceCollection services,
Config config) where T : class
{
SocketsHttpHandler socketsHandler = new SocketsHttpHandler()
{
PooledConnectionIdleTimeout = Timeout.InfiniteTimeSpan,
KeepAlivePingDelay = TimeSpan.FromSeconds(60),
KeepAlivePingTimeout = TimeSpan.FromSeconds(30),
EnableMultipleHttp2Connections = true
};
MethodConfig defaultMethodConfig = new MethodConfig
{
Names = { MethodName.Default },
RetryPolicy = new RetryPolicy
{
MaxAttempts = 5,
InitialBackoff = TimeSpan.FromSeconds(1),
MaxBackoff = TimeSpan.FromSeconds(5),
BackoffMultiplier = 1.5,
RetryableStatusCodes = { StatusCode.Unavailable }
}
};
ServiceConfig serviceConfig = new() { MethodConfigs = { defaultMethodConfig } };
services.AddGrpcClient<T>(o => {
o.Address = new Uri(config.GrpcUrl);
})
.ConfigureChannel(o => {
o.Credentials = GetGrpcClientCredentials(config);
o.ServiceConfig = serviceConfig;
})
.ConfigurePrimaryHttpMessageHandler(() => socketsHandler);
}