0
votes

Please help me to understand grpc client connection error handling.

I've used Micrsoft WCF before. It was easy to try to connect several times and to give up if there is no service accepting incoming connections like below:

// this is pseudo code
int attemptCount = 0;
while (true)
    try
    {
        client.Connect(); // exception is raised if there is not service listening for incoming connection 
        break;
    }
    catch (Exception)
    {
        client.Abort(); // to clear connection faulted state
        if (++attempCount == 5)
            throw;
        Thread.Wait(500); // waiting for service to start
    }

5 connection attempts is taken and then client application is terminated if there is no service listening on certain IP:port

This was used when I have client and service started from VisualStudio debug at the same time so sometime client starts first and it has to wait for service to start.

I've tried to do the same using gRPC client but there is no method to reset channel.State from ChannelState.TransientFailure back to working. I know that gRPC makes pauses between connection if something is wrong:

For many non-fatal failures (e.g., TCP connection attempts timing out because the server is not yet available), the channel may spend increasingly large amounts of time in this state. https://grpc.io/grpc-java/javadoc/io/grpc/ConnectivityState.html

I can try to use WaitForStateChangedAsync but how can I configure gRPC client to wait for certain amount of time between reconnection attempts ?

Is there any other way to connect several times and terminate client for gRPC ?

Thank you

1

1 Answers

0
votes

gRPC has exponential backoff control on channel reconnection internally.

We do have some backoff parameters, and some of them are configurable by channel arguments. https://github.com/grpc/grpc/blob/2bd7ad0112f56d2bdbc37d01a431c1e375039f2e/src/core/ext/filters/client_channel/subchannel.cc#L61

But we don't have any parameter to control the max attempt times as far as I know. Please file a feature request on https://github.com/grpc/grpc so that we can follow up.