Skip to content

Commit 15797e9

Browse files
committed
fix(rpc): add keepalive enforcement policy to server
Without an explicit KeepaliveEnforcementPolicy, the gRPC transport applies a default MinTime of 5 minutes and PermitWithoutStream=false. Any client that pings more frequently than every 5 minutes — or pings while idle — accumulates ping strikes and gets disconnected with a GOAWAY (too_many_pings) after three strikes.
1 parent 47a7c3a commit 15797e9

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

pkg/server/rpc/server/server.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ func New(cfg *config.ServerConfig) *Server {
6969
MaxConnectionAge: cfg.MaxLifeTime,
7070
})
7171

72+
// KeepaliveEnforcementPolicy limits how aggressively clients may send
73+
// keepalive pings. Without it, the transport applies a default MinTime of
74+
// 5 minutes and PermitWithoutStream=false, which rejects clients that ping
75+
// more frequently (e.g. every 60s) or ping while idle — sending GOAWAY
76+
// (too_many_pings) and breaking the connection.
77+
// Setting MinTime to KeepAliveInterval (60s) matches the client ping rate,
78+
// and PermitWithoutStream=true allows idle keepalive pings so clients can
79+
// proactively detect dead connections. This keeps both old and new clients
80+
// working: an old client (PermitWithoutStream=false) never pings while idle
81+
// and so never triggers a strike; a new client (PermitWithoutStream=true)
82+
// pings at 60s, within the allowed MinTime.
83+
keepaliveEnforcementOpts := grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
84+
MinTime: cfg.KeepAliveInterval,
85+
PermitWithoutStream: true,
86+
})
87+
7288
if cfg.MaxReceiveMessageSize > 0 {
7389
srv.serverOptions = append(srv.serverOptions, grpc.MaxRecvMsgSize(cfg.MaxReceiveMessageSize))
7490
}
@@ -79,7 +95,7 @@ func New(cfg *config.ServerConfig) *Server {
7995
interceptors.LoggerForUnaryServer(srv.config),
8096
interceptors.Metric(),
8197
)
82-
srv.serverOptions = append(srv.serverOptions, keepaliveOpts, srv.WithUnaryServerChain())
98+
srv.serverOptions = append(srv.serverOptions, keepaliveOpts, keepaliveEnforcementOpts, srv.WithUnaryServerChain())
8399
srv.server = grpc.NewServer(srv.serverOptions...)
84100
return srv
85101
}

0 commit comments

Comments
 (0)