From 0b2a17f83ab7845ad8203eff4c213773880108e6 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Fri, 7 Jan 2022 23:52:11 +0800 Subject: [PATCH] Feature: Add web monitor enable config item resolve #936 Signed-off-by: kwanhur --- bfe_config/bfe_conf/bfe_config_load_test.go | 4 ++-- bfe_config/bfe_conf/conf_basic.go | 16 +++++++++------- bfe_config/bfe_conf/conf_basic_test.go | 9 +++++---- bfe_config/bfe_conf/testdata/conf_all/bfe.conf | 2 ++ .../bfe_conf/testdata/conf_basic/bfe_1.conf | 2 ++ bfe_server/bfe_server_init.go | 12 +++++++----- conf/bfe.conf | 2 ++ 7 files changed, 29 insertions(+), 18 deletions(-) diff --git a/bfe_config/bfe_conf/bfe_config_load_test.go b/bfe_config/bfe_conf/bfe_config_load_test.go index eff72d913..1d7da19cf 100644 --- a/bfe_config/bfe_conf/bfe_config_load_test.go +++ b/bfe_config/bfe_conf/bfe_config_load_test.go @@ -29,7 +29,7 @@ func TestBfeConfigLoadNormal(t *testing.T) { t.Errorf("config.HttpPort should be 80") } - if config.Server.MonitorPort != 8080 { + if config.Server.MonitorEnabled && config.Server.MonitorPort != 8080 { t.Errorf("config.MonitorPort should be 8080") } @@ -90,7 +90,7 @@ func TestBfeConfigLoadUsingDefault(t *testing.T) { t.Errorf("config.HttpPort should be 8080") } - if config.Server.MonitorPort != 8421 { + if config.Server.MonitorEnabled && config.Server.MonitorPort != 8421 { t.Errorf("config.MonitorPort should be 8421") } diff --git a/bfe_config/bfe_conf/conf_basic.go b/bfe_config/bfe_conf/conf_basic.go index acfd94bcb..51a81daa1 100644 --- a/bfe_config/bfe_conf/conf_basic.go +++ b/bfe_config/bfe_conf/conf_basic.go @@ -38,11 +38,12 @@ const ( ) type ConfigBasic struct { - HttpPort int // listen port for http - HttpsPort int // listen port for https - MonitorPort int // web server port for monitor - MaxCpus int // number of max cpus to use - AcceptNum int // number of accept goroutine for each listener, default 1 + HttpPort int // listen port for http + HttpsPort int // listen port for https + MonitorPort int // web server port for monitor + MaxCpus int // number of max cpus to use + AcceptNum int // number of accept goroutine for each listener, default 1 + MonitorEnabled bool // web server for monitor enable or not // settings of layer-4 load balancer Layer4LoadBalancer string @@ -84,6 +85,7 @@ func (cfg *ConfigBasic) SetDefaultConf() { cfg.HttpPort = 8080 cfg.HttpsPort = 8443 cfg.MonitorPort = 8421 + cfg.MonitorEnabled = true cfg.MaxCpus = 0 cfg.TlsHandshakeTimeout = 30 @@ -141,8 +143,8 @@ func basicConfCheck(cfg *ConfigBasic) error { cfg.HttpsPort) } - // check MonitorPort - if cfg.MonitorPort < 1 || cfg.MonitorPort > 65535 { + // check MonitorPort if MonitorEnabled enabled + if cfg.MonitorEnabled && (cfg.MonitorPort < 1 || cfg.MonitorPort > 65535) { return fmt.Errorf("MonitorPort[%d] should be in [1, 65535]", cfg.MonitorPort) } diff --git a/bfe_config/bfe_conf/conf_basic_test.go b/bfe_config/bfe_conf/conf_basic_test.go index dd3dfb0c6..b3e07f820 100644 --- a/bfe_config/bfe_conf/conf_basic_test.go +++ b/bfe_config/bfe_conf/conf_basic_test.go @@ -53,7 +53,7 @@ func Test_conf_basic_case1(t *testing.T) { t.Error("config.HttpPort should be 80") } - if config.Server.MonitorPort != 8080 { + if config.Server.MonitorEnabled && config.Server.MonitorPort != 8080 { t.Error("config.MonitorPort should be 8080") } @@ -101,10 +101,11 @@ func Test_conf_basic_check(t *testing.T) { conf *ConfigBasic err string }{ - {&ConfigBasic{HttpPort: 80, HttpsPort: 443, MonitorPort: 8080, MaxCpus: -1}, "MaxCpus[-1] is too small"}, - {&ConfigBasic{HttpPort: 80, HttpsPort: 443, MonitorPort: 8080, MaxCpus: 10, TlsHandshakeTimeout: 30, + {&ConfigBasic{HttpPort: 80, HttpsPort: 443, MonitorPort: -1, MonitorEnabled: true},"MonitorPort[-1] should be in [1, 65535]"}, + {&ConfigBasic{HttpPort: 80, HttpsPort: 443, MonitorPort: 8080, MonitorEnabled: false, MaxCpus: -1}, "MaxCpus[-1] is too small"}, + {&ConfigBasic{HttpPort: 80, HttpsPort: 443, MonitorPort: 8080, MonitorEnabled: true, MaxCpus: 10, TlsHandshakeTimeout: 30, GracefulShutdownTimeout: 30}, "ClientReadTimeout[0] should > 0"}, - {&ConfigBasic{HttpPort: 80, HttpsPort: 443, MonitorPort: 8080, MaxCpus: 10, TlsHandshakeTimeout: 30, + {&ConfigBasic{HttpPort: 80, HttpsPort: 443, MonitorPort: 8080, MonitorEnabled: true, MaxCpus: 10, TlsHandshakeTimeout: 30, GracefulShutdownTimeout: 30, ClientReadTimeout: 10, ClientWriteTimeout: 10, MonitorInterval: 33}, "MonitorInterval[33] can not divide 60"}, } diff --git a/bfe_config/bfe_conf/testdata/conf_all/bfe.conf b/bfe_config/bfe_conf/testdata/conf_all/bfe.conf index a2c2f18f7..e1510e7bb 100644 --- a/bfe_config/bfe_conf/testdata/conf_all/bfe.conf +++ b/bfe_config/bfe_conf/testdata/conf_all/bfe.conf @@ -5,6 +5,8 @@ httpPort = 80 httpsPort = 443 # listen port for monitor request monitorPort = 8080 +# enable monitor server +MonitorEnabled = true # max number of CPUs to use maxCpus = 2 diff --git a/bfe_config/bfe_conf/testdata/conf_basic/bfe_1.conf b/bfe_config/bfe_conf/testdata/conf_basic/bfe_1.conf index 5519d5b2e..2eccbc3d9 100644 --- a/bfe_config/bfe_conf/testdata/conf_basic/bfe_1.conf +++ b/bfe_config/bfe_conf/testdata/conf_basic/bfe_1.conf @@ -5,6 +5,8 @@ httpPort = 80 httpsPort = 443 # listen port for monitor request monitorPort = 8080 +# enable monitor server +MonitorEnabled = true # max number of CPUs to use maxCpus = 5 diff --git a/bfe_server/bfe_server_init.go b/bfe_server/bfe_server_init.go index be2c82306..ab3f2e539 100644 --- a/bfe_server/bfe_server_init.go +++ b/bfe_server/bfe_server_init.go @@ -58,11 +58,13 @@ func StartUp(cfg bfe_conf.BfeConfig, version string, confRoot string) error { bfeServer.InitSignalTable() log.Logger.Info("StartUp():bfeServer.InitSignalTable() OK") - // init web monitor - monitorPort := cfg.Server.MonitorPort - if err = bfeServer.InitWebMonitor(monitorPort); err != nil { - log.Logger.Error("StartUp(): InitWebMonitor():%s", err.Error()) - return err + // init web monitor if enabled + if cfg.Server.MonitorEnabled { + monitorPort := cfg.Server.MonitorPort + if err = bfeServer.InitWebMonitor(monitorPort); err != nil { + log.Logger.Error("StartUp(): InitWebMonitor():%s", err.Error()) + return err + } } // register modules diff --git a/conf/bfe.conf b/conf/bfe.conf index c399ea295..0b34d1f7d 100644 --- a/conf/bfe.conf +++ b/conf/bfe.conf @@ -5,6 +5,8 @@ HttpPort = 8080 HttpsPort = 8443 # listen port for monitor request MonitorPort = 8421 +# if false, disable monitor server +MonitorEnabled = true # max number of CPUs to use (0 to use all CPUs) MaxCpus = 0