Check if gRpc server is running in C#
Asked Answered
F

2

6

I am writing a gRpc server in C# and I wanted to add automatic recovery/retry implementation if server is down for any reason. Upon researching, i came across interceptors and got excited but looks like it is supported only for Go. I cannot find anything similar for gRpc C#.

How can automatic recovery be handled in gRpc CSharp or is there any similar concept like interceptors that we can use ?

EDIT- in GRPC c#, is there a way to check if the server is running or available ?

Forgive answered 7/7, 2020 at 6:13 Comment(2)
Is this the Interceptor that you're referring to?Trudeau
Did you get the answer?Scotland
Y
3

I am not sure if this is the good practice and correct usage of API, but I would implement this using Channel instance of your client. So in client class I would expose IsReady method for trying to connect and checking the state of the channel afterwards. Here is a code example:

public bool IsReady()
{
  channel.ConnectAsync();
  return channel.State == ChannelState.Ready;
}

You can also implement async version of this method with passing deadline for connection:

public async Task<bool> IsReadyAsync(DateTime? deadline)
{
  try
  {
    await channel.ConnectAsync(deadline: deadline);
  }
  catch (TaskCanceledException)
  {
    return false;
  }

  return channel.State == ChannelState.Ready;
}

ConnectAsync method requests channel to connect service without starting an RPC. Returned task completes once channel state is Ready. If the deadline is reached, or channel enters the Shutdown state, the task is cancelled. So you can play around with this to achieve your goal or use examples provided if they will work for you.

Yelenayelich answered 3/8, 2020 at 9:37 Comment(0)
F
0

-The best concept I know is to use HealthCheck to check if the server is ok :

https://learn.microsoft.com/en-us/aspnet/core/grpc/health-checks?view=aspnetcore-7.0

-For error management in server you can use a classic try catch in server side :

Try { start server } catch{ restart server}

Fai answered 6/10, 2023 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.