diff --git a/internal/peer/node/start.go b/internal/peer/node/start.go index 3b8a661ecd9..0983dd6b78a 100644 --- a/internal/peer/node/start.go +++ b/internal/peer/node/start.go @@ -1163,7 +1163,7 @@ func secureDialOpts(credSupport *comm.CredentialSupport) func() []grpc.DialOptio if viper.IsSet("peer.keepalive.client.timeout") { kaOpts.ClientTimeout = viper.GetDuration("peer.keepalive.client.timeout") } - dialOpts = append(dialOpts, comm.ClientKeepaliveOptions(kaOpts)...) + dialOpts = append(dialOpts, kaOpts.ClientKeepaliveOptions()...) if viper.GetBool("peer.tls.enabled") { dialOpts = append(dialOpts, grpc.WithTransportCredentials(credSupport.GetPeerCredentials())) diff --git a/internal/pkg/comm/config.go b/internal/pkg/comm/config.go index 25e7063bea8..ba3a7408763 100644 --- a/internal/pkg/comm/config.go +++ b/internal/pkg/comm/config.go @@ -134,15 +134,8 @@ type KeepaliveOptions struct { ServerMinInterval time.Duration } -type Metrics struct { - // OpenConnCounter keeps track of number of open connections - OpenConnCounter metrics.Counter - // ClosedConnCounter keeps track of number connections closed - ClosedConnCounter metrics.Counter -} - -// ServerKeepaliveOptions returns gRPC keepalive options for server. -func ServerKeepaliveOptions(ka KeepaliveOptions) []grpc.ServerOption { +// ServerKeepaliveOptions returns gRPC keepalive options for a server. +func (ka KeepaliveOptions) ServerKeepaliveOptions() []grpc.ServerOption { var serverOpts []grpc.ServerOption kap := keepalive.ServerParameters{ Time: ka.ServerInterval, @@ -158,8 +151,8 @@ func ServerKeepaliveOptions(ka KeepaliveOptions) []grpc.ServerOption { return serverOpts } -// ClientKeepaliveOptions returns gRPC keepalive options for clients. -func ClientKeepaliveOptions(ka KeepaliveOptions) []grpc.DialOption { +// ClientKeepaliveOptions returns gRPC keepalive dial options for clients. +func (ka KeepaliveOptions) ClientKeepaliveOptions() []grpc.DialOption { var dialOpts []grpc.DialOption kap := keepalive.ClientParameters{ Time: ka.ClientInterval, @@ -169,3 +162,10 @@ func ClientKeepaliveOptions(ka KeepaliveOptions) []grpc.DialOption { dialOpts = append(dialOpts, grpc.WithKeepaliveParams(kap)) return dialOpts } + +type Metrics struct { + // OpenConnCounter keeps track of number of open connections + OpenConnCounter metrics.Counter + // ClosedConnCounter keeps track of number connections closed + ClosedConnCounter metrics.Counter +} diff --git a/internal/pkg/comm/config_test.go b/internal/pkg/comm/config_test.go index 00979134b82..6ea5d0884d4 100644 --- a/internal/pkg/comm/config_test.go +++ b/internal/pkg/comm/config_test.go @@ -30,7 +30,7 @@ func TestServerKeepaliveOptions(t *testing.T) { grpc.KeepaliveParams(kap), grpc.KeepaliveEnforcementPolicy(kep), } - opts := ServerKeepaliveOptions(DefaultKeepaliveOptions) + opts := DefaultKeepaliveOptions.ServerKeepaliveOptions() // Unable to test equality of options since the option methods return // functions and each instance is a different func. @@ -52,7 +52,7 @@ func TestClientKeepaliveOptions(t *testing.T) { PermitWithoutStream: true, } expectedOpts := []grpc.DialOption{grpc.WithKeepaliveParams(kap)} - opts := ClientKeepaliveOptions(DefaultKeepaliveOptions) + opts := DefaultKeepaliveOptions.ClientKeepaliveOptions() // Unable to test equality of options since the option methods return // functions and each instance is a different func. diff --git a/internal/pkg/comm/server.go b/internal/pkg/comm/server.go index 68c2e12d8e5..3ee87b987fa 100644 --- a/internal/pkg/comm/server.go +++ b/internal/pkg/comm/server.go @@ -127,7 +127,7 @@ func NewGRPCServerFromListener(listener net.Listener, serverConfig ServerConfig) serverOpts = append(serverOpts, grpc.MaxSendMsgSize(DefaultMaxSendMsgSize)) serverOpts = append(serverOpts, grpc.MaxRecvMsgSize(DefaultMaxRecvMsgSize)) // set the keepalive options - serverOpts = append(serverOpts, ServerKeepaliveOptions(serverConfig.KaOpts)...) + serverOpts = append(serverOpts, serverConfig.KaOpts.ServerKeepaliveOptions()...) // set connection timeout if serverConfig.ConnectionTimeout <= 0 { serverConfig.ConnectionTimeout = DefaultConnectionTimeout