@@ -3,6 +3,7 @@ package rpc
33import (
44 "crypto/tls"
55 "fmt"
6+ "math"
67 "math/rand"
78 "net"
89 "net/http"
@@ -23,6 +24,7 @@ import (
2324 "go.temporal.io/server/temporal/environment"
2425 "google.golang.org/grpc"
2526 "google.golang.org/grpc/credentials"
27+ "google.golang.org/grpc/keepalive"
2628)
2729
2830var _ common.RPCFactory = (* RPCFactory )(nil )
@@ -45,6 +47,10 @@ type RPCFactory struct {
4547 // A OnceValues wrapper for createLocalFrontendHTTPClient.
4648 localFrontendClient func () (* common.FrontendHTTPClient , error )
4749 interNodeGrpcConnections cache.Cache
50+
51+ // TODO: Remove these flags once the keepalive settings are rolled out
52+ EnableInternodeServerKeepalive bool
53+ EnableInternodeClientKeepalive bool
4854}
4955
5056// NewFactory builds a new RPCFactory
@@ -115,6 +121,12 @@ func (d *RPCFactory) GetRemoteClusterClientConfig(hostname string) (*tls.Config,
115121func (d * RPCFactory ) GetInternodeGRPCServerOptions () ([]grpc.ServerOption , error ) {
116122 var opts []grpc.ServerOption
117123
124+ if d .EnableInternodeServerKeepalive {
125+ rpcConfig := d .config .Services [string (d .serviceName )].RPC
126+ kep := rpcConfig .KeepAliveServerConfig .GetKeepAliveEnforcementPolicy ()
127+ kp := rpcConfig .KeepAliveServerConfig .GetKeepAliveServerParameters ()
128+ opts = append (opts , grpc .KeepaliveEnforcementPolicy (kep ), grpc .KeepaliveParams (kp ))
129+ }
118130 if d .tlsFactory != nil {
119131 serverConfig , err := d .tlsFactory .GetInternodeServerConfig ()
120132 if err != nil {
@@ -126,11 +138,6 @@ func (d *RPCFactory) GetInternodeGRPCServerOptions() ([]grpc.ServerOption, error
126138 opts = append (opts , grpc .Creds (credentials .NewTLS (serverConfig )))
127139 }
128140
129- rpcConfig := d .config .Services [string (d .serviceName )].RPC
130- kep := rpcConfig .KeepAliveServerConfig .GetKeepAliveEnforcementPolicy ()
131- kp := rpcConfig .KeepAliveServerConfig .GetKeepAliveServerParameters ()
132- opts = append (opts , grpc .KeepaliveEnforcementPolicy (kep ), grpc .KeepaliveParams (kp ))
133-
134141 return opts , nil
135142}
136143
@@ -251,8 +258,17 @@ func (d *RPCFactory) dial(hostName string, tlsClientConfig *tls.Config, dialOpti
251258}
252259
253260func (d * RPCFactory ) getClientKeepAliveConfig (serviceName primitives.ServiceName ) grpc.DialOption {
254- serviceConfig := d .config .Services [string (serviceName )]
255- return grpc .WithKeepaliveParams (serviceConfig .RPC .ClientConnectionConfig .GetKeepAliveClientParameters ())
261+ // default keepalive settings for clients
262+ params := keepalive.ClientParameters {
263+ Time : time .Duration (math .MaxInt64 ),
264+ Timeout : 20 * time .Second ,
265+ PermitWithoutStream : false ,
266+ }
267+ if d .EnableInternodeClientKeepalive {
268+ serviceConfig := d .config .Services [string (serviceName )]
269+ params = serviceConfig .RPC .ClientConnectionConfig .GetKeepAliveClientParameters ()
270+ }
271+ return grpc .WithKeepaliveParams (params )
256272}
257273
258274func (d * RPCFactory ) GetTLSConfigProvider () encryption.TLSConfigProvider {
0 commit comments