2

I am trying to understand how keepalive or idle connection works with gRPC. I have bidirectional streaming RPC, where I create session and do nothing so that there is no activity on the channel.

  1. If there is no activity, GRPC_ARG_KEEPALIVE_TIME_MS signal will be blocked (https://github.com/grpc/grpc/blob/master/doc/keepalive.md#faq) and connection will be closed after this interval, however, it does not terminate and I see keepalive ping is sent and received. why?

  2. If we do not set any params, is there any timeout after which connection will be automatically closed? If yes, how do I change this behaviour, which param?

debonair
  • 2,505
  • 4
  • 33
  • 73
  • 1
    re 1) I think the ping is not blocked because you have an RPC open - the ping will only not be sent if GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS is false and you don't have any RPCs active – apolcyn Jan 20 '22 at 20:45
  • 1
    2) there are server-controlled connection timeouts like GRPC_ARG_MAX_CONNECTION_AGE_MS and GRPC_ARG_MAX_CONNECTION_IDLE_MS (see grpc_types.h comments for documentation), which can also influence how long a connection lasts. Those are configured by servers only though. – apolcyn Jan 20 '22 at 20:47
  • @apolcyn you should have added this as an answer :) – debonair Jan 25 '22 at 04:29

2 Answers2

2

What is happening here is that maybe GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS parameter is true that's why it is sending the pings even without connection.

GRPC_ARG_KEEPALIVE_TIMEOUT_MS is controlling the timeout as written in the documentation:

This channel argument controls the amount of time (in milliseconds) the sender of the keepalive ping waits for an acknowledgement. If it does not receive an acknowledgment within this time, it will close the connection.

You can manually adjust this parameter to control the timeout duration.

Set the GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS to false so it cannot send keepAlive pings when there is no active call.

Abdul ahad
  • 1,383
  • 7
  • 18
0

A KeepAlive parameter is what's used to ensure the connection stays open and to indicate to the server and client that it's still connected and responding.

You can see a low-level view of this in action here.

On the other hand, the Idle Connections parameter closes connections after no requests have been sent over the connection (not the keep-alive packets, this refers to the application level packets). This is good since idle connections occupy memory, CPU, and an open socket, all finite resources.

Noah
  • 859
  • 7
  • 17