gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/Documentation/keepalive.md (about)

     1  # Keepalive
     2  
     3  gRPC sends http2 pings on the transport to detect if the connection is down. If
     4  the ping is not acknowledged by the other side within a certain period, the
     5  connection will be closed. Note that pings are only necessary when there's no
     6  activity on the connection.
     7  
     8  For how to configure keepalive, see
     9  https://godoc.org/google.golang.org/grpc/keepalive for the options.
    10  
    11  ## Why do I need this?
    12  
    13  Keepalive can be useful to detect TCP level connection failures. A particular
    14  situation is when the TCP connection drops packets (including FIN). It would
    15  take the system TCP timeout (which can be 30 minutes) to detect this failure.
    16  Keepalive would allow gRPC to detect this failure much sooner.
    17  
    18  Another usage is (as the name suggests) to keep the connection alive. For
    19  example in cases where the L4 proxies are configured to kill "idle" connections.
    20  Sending pings would make the connections not "idle".
    21  
    22  ## What should I set?
    23  
    24  It should be sufficient for most users to set [client
    25  parameters](https://godoc.org/google.golang.org/grpc/keepalive) as a [dial
    26  option](https://godoc.org/google.golang.org/grpc#WithKeepaliveParams).
    27  
    28  ## What will happen?
    29  
    30  (The behavior described here is specific for gRPC-go, it might be slightly
    31  different in other languages.)
    32  
    33  When there's no activity on a connection (note that an ongoing stream results in
    34  __no activity__ when there's no message being sent), after `Time`, a ping will
    35  be sent by the client and the server will send a ping ack when it gets the ping.
    36  Client will wait for `Timeout`, and check if there's any activity on the
    37  connection during this period (a ping ack is an activity).
    38  
    39  ## What about server side?
    40  
    41  Server has similar `Time` and `Timeout` settings as client. Server can also
    42  configure connection max-age. See [server
    43  parameters](https://godoc.org/google.golang.org/grpc/keepalive#ServerParameters)
    44  for details.
    45  
    46  ### Enforcement policy
    47  
    48  [Enforcement
    49  policy](https://godoc.org/google.golang.org/grpc/keepalive#EnforcementPolicy) is
    50  a special setting on server side to protect server from malicious or misbehaving
    51  clients.
    52  
    53  Server sends GOAWAY with ENHANCE_YOUR_CALM and close the connection when bad
    54  behaviors are detected:
    55   - Client sends too frequent pings
    56   - Client sends pings when there's no stream and this is disallowed by server
    57     config