Working with Keep-Alive

1. Configuring Client Keep-Alive

FieldDefaultDescription
Time∞ (disabled)Idle interval before ping
Timeout20sPong wait
PermitWithoutStreamfalsePing with no active RPCs

2. Setting Keep-Alive Time

RecommendationDetail
Production10–30s
Behind LBLess than LB idle timeout
Mobile30–60s to save battery

3. Setting Keep-Alive Timeout

ValueEffect
Too shortFalse disconnects under load
Too longSlow dead-peer detection
Typical3–5s

4. Allowing Pings Without Stream

Example: Idle pings

kp := keepalive.ClientParameters{
    Time: 10*time.Second, Timeout: 3*time.Second,
    PermitWithoutStream: true,
}
Warning: Server enforcement may close connections sending too many pings. Coordinate client + server values.

5. Configuring Server Keep-Alive

Example: Server keep-alive

srv := grpc.NewServer(
    grpc.KeepaliveParams(keepalive.ServerParameters{
        MaxConnectionIdle:     5 * time.Minute,
        MaxConnectionAge:      30 * time.Minute,
        MaxConnectionAgeGrace: 30 * time.Second,
        Time:                  10 * time.Second,
        Timeout:               3 * time.Second,
    }),
)
FieldPurpose
MaxConnectionIdleClose idle connections
MaxConnectionAgeForce re-LB by closing old connections
MaxConnectionAgeGraceDrain time before close

6. Setting Enforcement Policy

Example: Enforcement

grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
    MinTime:             5 * time.Second,
    PermitWithoutStream: true,
})
FieldEffect
MinTimeMin interval between client pings
PermitWithoutStreamAllow client pings during idle

7. Setting Min Time Between Pings

SymptomCause
Client ENHANCE_YOUR_CALMServer enforced ping limit exceeded
FixIncrease client Time ≥ server MinTime

8. Permitting Without Stream on Server

SettingWhen
trueServer allows pings on idle connections
false (default)Server only accepts pings with active stream

9. Detecting Connection Loss

SideDetection
ClientPing timeout → close + reconnect
ServerHTTP/2 frame errors / TCP RST

10. Tuning Keep-Alive for Production

ScenarioRecommended
Internal meshTime 10s / Timeout 3s
Behind L4 LBTime < LB idle (typically 60–300s)
MobileTime 30–60s
MinTime alignmentServer MinTime ≤ client Time