Skip to content

Commit

Permalink
add grpc keep-alive support
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Jul 26, 2019
1 parent d8e9696 commit af942f5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server/hdl_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ func serveGrpc(addr string, tlsConf *tls.Config) (*grpc.Server, error) {
opts = append(opts, grpc.Creds(credentials.NewTLS(tlsConf)))
secure = " secure"
}

kepConfig := keepalive.EnforcementPolicy{
MinTime: 1 * time.Second, // If a client pings more than once every second, terminate the connection
PermitWithoutStream: true, // Allow pings even when there are no active streams
}

kpConfig := keepalive.ServerParameters{
MaxConnectionIdle: 15 * time.Second, // If a client is idle for 15 seconds, send a GOAWAY
MaxConnectionAge: 30 * time.Second, // If any connection is alive for more than 30 seconds, send a GOAWAY
MaxConnectionAgeGrace: 5 * time.Second, // Allow 5 seconds for pending RPCs to complete before forcibly closing connections
Time: 5 * time.Second, // Ping the client if it is idle for 5 seconds to ensure the connection is still active
Timeout: 1 * time.Second, // Wait 1 second for the ping ack before assuming the connection is dead
}
opts = append(opts, grpc.KeepaliveEnforcementPolicy(kepConfig))
opts = append(opts, grpc.KeepaliveParams(kpConfig))

srv := grpc.NewServer(opts...)
pbx.RegisterNodeServer(srv, &grpcNodeServer{})
log.Printf("gRPC/%s%s server is registered at [%s]", grpc.Version, secure, addr)
Expand Down

0 comments on commit af942f5

Please sign in to comment.