how python grpc client manage connections?
Asked Answered
F

1

5

When exactly a grpc client create and close connections?
I begin the code with:

channel = grpc.insecure_channel('localhost:8888')
stub = myservice_pb2_grpc.MyServiceStub(channel)

Does declaring a channel creates a single socket for the entire lifetime of the process?
Cause even if I give invalid address to insecure_channel() I don't see any error until I make the first request.

Or, the socket is created only when request is made and closed afterwards?

Faceless answered 25/6, 2020 at 8:4 Comment(0)
E
7

In gRPC Python a channel object corresponds to one or more TCP connections, depending on your load balancing policy. If no load balancing policy is selected (which seems to be the vast majority of usage), then yes, a channel corresponds to a single TCP connection.

The connections represented by a channel will remain alive as long as the channel object itself is open. It is therefore recommended that you reuse channels across many RPC invocations with a client process. It is also recommended that you close a channel once it is no longer needed.

There are two ways to accomplish this. The first is to manually call the close method:

channel = grpc.insecure_channel('localhost:8888')
# send some RPCs
channel.close()

The other is to use the context manager:

with grpc.insecure_channel('localhost:8888') as channel:
  # send some RPCs

Elliellicott answered 6/7, 2020 at 17:37 Comment(3)
Does the internal socket has a timeout if it is idle for some time? Or does gRPC have a reconnect / connection pool mechanism? I could not find the relevant part in the source code.Faceless
Yes. A channel may enter an IDLE state after a period of time, during which no TCP connection is active. This period can be configured with the "grpc.client_idle_timeout_ms" channel argument. Supply this to the options parameter when creating your channel. By default, it is set to 30 minutes. The channel will reconnect if another RPC is sent on the channel.Elliellicott
The languages based on the C++ Core imlementation (C++, Python, Ruby, C#, PHP, etc.) also have "subchannel pooling". A subchannel always corresponds to a single TCP connection regardless of load balancing policy and subchannels may be shared by multiple channels. So if two channels are created to the same backend with the same configuration, they will share a single subchannel and therefore a single TCP connection. Something similar may exist in Java/Go, but I don't know for certain.Elliellicott

© 2022 - 2024 — McMap. All rights reserved.