Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a GRPC server is available

Tags:

python

linux

grpc

If a gRPC client starts before the server, it exits with an exception. The application I am working on starts a bunch of servers and clients (near) simultaneously, however, and I have to be robust with respect to execution order. If the client starts before the server, I want it to wait until the server shows up.

I modified the HelloWorld python client example as follows:

done = False
while not done:
    try:
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), timeout=1)
        print("Greeter client received: " + response.message)
        done = True
    except:
        print('Waiting for server connect...')
        time.sleep(1)

so now if I start the client before the server, I get the 'Waiting for server connect...' message scrolling up my terminal window as expected. Then I start the server, and it connects... eventually. In fact it takes about ten seconds before the 'Hello you' messages appears, which is surprisingly long. Why might it be so slow, and is there a robust way to check for server connection?

like image 550
Julian Gold Avatar asked Aug 18 '17 14:08

Julian Gold


People also ask

Can a gRPC server also be a client?

Yes, you can definitely do that.

Is gRPC a server?

gRPC is a robust open-source RPC (Remote Procedure Call) framework used to build scalable and fast APIs. It allows the client and server applications to communicate transparently and develop connected systems. Many leading tech firms have adopted gRPC, such as Google, Netflix, Square, IBM, Cisco, & Dropbox.


1 Answers

Take a look at ChannelConnectivity - I am a go programmer but it should be the same in python. What I do is create while/for loop and when the "ChannelConnectivity" is set to "READY" I create the client from the connection and then continue.

like image 189
Trevor V Avatar answered Oct 11 '22 15:10

Trevor V