diff --git a/core/peer/config.go b/core/peer/config.go index 486ce8ebae0..9b7249b4345 100644 --- a/core/peer/config.go +++ b/core/peer/config.go @@ -129,7 +129,10 @@ func GetServerConfig() (comm.ServerConfig, error) { secureOptions := &comm.SecureOptions{ UseTLS: viper.GetBool("peer.tls.enabled"), } - serverConfig := comm.ServerConfig{SecOpts: secureOptions} + serverConfig := comm.ServerConfig{ + ConnectionTimeout: viper.GetDuration("peer.connectiontimeout"), + SecOpts: secureOptions, + } if secureOptions.UseTLS { // get the certs from the file system serverKey, err := ioutil.ReadFile(config.GetPath("peer.tls.key.file")) diff --git a/core/peer/config_test.go b/core/peer/config_test.go index 13a7b605978..0c13704e4ae 100644 --- a/core/peer/config_test.go +++ b/core/peer/config_test.go @@ -133,9 +133,10 @@ func TestGetServerConfig(t *testing.T) { // good config without TLS viper.Set("peer.tls.enabled", false) + viper.Set("peer.connectiontimeout", "7s") sc, _ := GetServerConfig() - assert.Equal(t, false, sc.SecOpts.UseTLS, - "ServerConfig.SecOpts.UseTLS should be false") + assert.Equal(t, false, sc.SecOpts.UseTLS, "ServerConfig.SecOpts.UseTLS should be false") + assert.Equal(t, sc.ConnectionTimeout, 7*time.Second, "ServerConfig.ConnectionTimeout should be 7 seconds") // keepalive options assert.Equal(t, comm.DefaultKeepaliveOptions, sc.KaOpts, diff --git a/orderer/common/localconfig/config.go b/orderer/common/localconfig/config.go index e33347842c4..d83bda87712 100644 --- a/orderer/common/localconfig/config.go +++ b/orderer/common/localconfig/config.go @@ -40,21 +40,22 @@ type TopLevel struct { // General contains config which should be common among all orderer types. type General struct { - LedgerType string - ListenAddress string - ListenPort uint16 - TLS TLS - Cluster Cluster - Keepalive Keepalive - GenesisMethod string - GenesisProfile string - SystemChannel string - GenesisFile string - Profile Profile - LocalMSPDir string - LocalMSPID string - BCCSP *bccsp.FactoryOpts - Authentication Authentication + LedgerType string + ListenAddress string + ListenPort uint16 + TLS TLS + Cluster Cluster + Keepalive Keepalive + ConnectionTimeout time.Duration + GenesisMethod string + GenesisProfile string + SystemChannel string + GenesisFile string + Profile Profile + LocalMSPDir string + LocalMSPID string + BCCSP *bccsp.FactoryOpts + Authentication Authentication } type Cluster struct { diff --git a/orderer/common/localconfig/config_test.go b/orderer/common/localconfig/config_test.go index a7e24dbaf05..0e3ed08ce9f 100644 --- a/orderer/common/localconfig/config_test.go +++ b/orderer/common/localconfig/config_test.go @@ -213,3 +213,25 @@ Consensus: assert.Equal(t, foo.Foo, "bar") assert.Equal(t, foo.Hello.World, 42) } + +func TestConnectionTimeout(t *testing.T) { + t.Run("without connection timeout overridden", func(t *testing.T) { + cleanup := configtest.SetDevFabricConfigPath(t) + defer cleanup() + cfg, err := Load() + assert.NotNil(t, cfg, "Could not load config") + assert.NoError(t, err, "Load good config returned unexpected error") + assert.Equal(t, cfg.General.ConnectionTimeout, 0*time.Second) + }) + + t.Run("with connection timeout overridden", func(t *testing.T) { + os.Setenv("ORDERER_GENERAL_CONNECTIONTIMEOUT", "10s") + defer os.Unsetenv("ORDERER_GENERAL_CONNECTIONTIMEOUT") + cleanup := configtest.SetDevFabricConfigPath(t) + defer cleanup() + cfg, err := Load() + assert.NotNil(t, cfg, "Could not load config") + assert.NoError(t, err, "Load good config returned unexpected error") + assert.Equal(t, cfg.General.ConnectionTimeout, 10*time.Second) + }) +} diff --git a/orderer/common/server/main.go b/orderer/common/server/main.go index b975da5efb7..93ff1eca1c9 100644 --- a/orderer/common/server/main.go +++ b/orderer/common/server/main.go @@ -490,10 +490,11 @@ func initializeServerConfig(conf *localconfig.TopLevel, metricsProvider metrics. } return comm.ServerConfig{ - SecOpts: secureOpts, - KaOpts: kaOpts, - Logger: commLogger, - MetricsProvider: metricsProvider, + SecOpts: secureOpts, + KaOpts: kaOpts, + Logger: commLogger, + MetricsProvider: metricsProvider, + ConnectionTimeout: conf.General.ConnectionTimeout, StreamInterceptors: []grpc.StreamServerInterceptor{ grpcmetrics.StreamServerInterceptor(grpcmetrics.NewStreamMetrics(metricsProvider)), grpclogging.StreamServerInterceptor(flogging.MustGetLogger("comm.grpc.server").Zap()), diff --git a/orderer/common/server/main_test.go b/orderer/common/server/main_test.go index 4a251062904..0b2ffcf1e7c 100644 --- a/orderer/common/server/main_test.go +++ b/orderer/common/server/main_test.go @@ -90,6 +90,7 @@ func TestInitializeProfilingService(t *testing.T) { func TestInitializeServerConfig(t *testing.T) { conf := &localconfig.TopLevel{ General: localconfig.General{ + ConnectionTimeout: 7 * time.Second, TLS: localconfig.TLS{ Enabled: true, ClientAuthRequired: true, @@ -105,6 +106,7 @@ func TestInitializeServerConfig(t *testing.T) { assert.Equal(t, defaultOpts.ServerMinInterval, sc.KaOpts.ServerMinInterval) assert.Equal(t, time.Duration(0), sc.KaOpts.ServerInterval) assert.Equal(t, time.Duration(0), sc.KaOpts.ServerTimeout) + assert.Equal(t, 7*time.Second, sc.ConnectionTimeout) testDuration := 10 * time.Second conf.General.Keepalive = localconfig.Keepalive{ ServerMinInterval: testDuration,