From cf845187774b427078d0c85848389ec06a4d9c2f Mon Sep 17 00:00:00 2001 From: lbbniu Date: Thu, 11 Jan 2024 22:59:23 +0800 Subject: [PATCH] perf: optimize service configuration file parse --- tars/application.go | 132 +++++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 62 deletions(-) diff --git a/tars/application.go b/tars/application.go index 60c1a1b8..02b5c50a 100755 --- a/tars/application.go +++ b/tars/application.go @@ -83,6 +83,8 @@ func init() { func newApp() *application { return &application{ opt: &options{}, + cltCfg: newClientConfig(), + svrCfg: newServerConfig(), tarsConfig: make(map[string]*transport.TarsServerConf), goSvrs: make(map[string]*transport.TarsServer), httpSvrs: make(map[string]*http.Server), @@ -123,8 +125,6 @@ func (a *application) initConfig() { }) }() }() - a.svrCfg = newServerConfig() - a.cltCfg = newClientConfig() if ServerConfigPath == "" { svrFlag := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) svrFlag.StringVar(&ServerConfigPath, "config", "", "server config path") @@ -140,9 +140,32 @@ func (a *application) initConfig() { TLOG.Errorf("Parse server config fail %v", err) return } + + // parse config + a.parseServerConfig(c) + a.parseClientConfig(c) a.conf = c - // Config.go + cachePath := filepath.Join(a.svrCfg.DataPath, a.svrCfg.Server) + ".tarsdat" + if cacheData, err := os.ReadFile(cachePath); err == nil { + _ = json.Unmarshal(cacheData, &a.appCache) + } + // cache + a.appCache.TarsVersion = Version + if a.svrCfg.LogLevel == "" { + a.svrCfg.LogLevel = a.appCache.LogLevel + } else { + a.appCache.LogLevel = a.svrCfg.LogLevel + } + rogger.SetLevel(rogger.StringToLevel(a.svrCfg.LogLevel)) + if a.svrCfg.LogPath != "" { + _ = TLOG.SetFileRoller(a.svrCfg.LogPath+"/"+a.svrCfg.App+"/"+a.svrCfg.Server, int(a.svrCfg.LogNum), int(a.svrCfg.LogSize)) + } + protocol.SetMaxPackageLength(a.svrCfg.MaxPackageLength) + _, _ = maxprocs.Set(maxprocs.Logger(TLOG.Infof)) +} + +func (a *application) parseServerConfig(c *conf.Conf) { // init server config if strings.EqualFold(c.GetString("/tars/application"), "Y") { a.svrCfg.Enableset = true @@ -173,24 +196,6 @@ func (a *application) initConfig() { // add adapters config a.svrCfg.Adapters = make(map[string]adapterConfig) - cachePath := filepath.Join(a.svrCfg.DataPath, a.svrCfg.Server) + ".tarsdat" - if cacheData, err := os.ReadFile(cachePath); err == nil { - _ = json.Unmarshal(cacheData, &a.appCache) - } - - if a.svrCfg.LogLevel == "" { - a.svrCfg.LogLevel = a.appCache.LogLevel - } else { - a.appCache.LogLevel = a.svrCfg.LogLevel - } - rogger.SetLevel(rogger.StringToLevel(a.svrCfg.LogLevel)) - if a.svrCfg.LogPath != "" { - _ = TLOG.SetFileRoller(a.svrCfg.LogPath+"/"+a.svrCfg.App+"/"+a.svrCfg.Server, int(a.svrCfg.LogNum), int(a.svrCfg.LogSize)) - } - - // cache - a.appCache.TarsVersion = Version - // add timeout config a.svrCfg.AcceptTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/server", AcceptTimeout)) a.svrCfg.ReadTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/server", ReadTimeout)) @@ -214,11 +219,14 @@ func (a *application) initConfig() { a.svrCfg.StatReportChannelBufLen = c.GetInt32WithDef("/tars/application/server", StatReportChannelBufLen) // maxPackageLength a.svrCfg.MaxPackageLength = c.GetIntWithDef("/tars/application/server", MaxPackageLength) - protocol.SetMaxPackageLength(a.svrCfg.MaxPackageLength) + // tls a.svrCfg.Key = c.GetString("/tars/application/server") a.svrCfg.Cert = c.GetString("/tars/application/server") - var tlsConfig *tls.Config + var ( + tlsConfig *tls.Config + err error + ) if a.svrCfg.Key != "" && a.svrCfg.Cert != "" { a.svrCfg.CA = c.GetString("/tars/application/server") a.svrCfg.VerifyClient = c.GetStringWithDef("/tars/application/server", "0") != "0" @@ -233,39 +241,6 @@ func (a *application) initConfig() { a.svrCfg.SampleAddress = c.GetString("/tars/application/server") a.svrCfg.SampleEncoding = c.GetStringWithDef("/tars/application/server", "json") - // init client config - cMap := c.GetMap("/tars/application/client") - a.cltCfg.Locator = cMap["locator"] - a.cltCfg.Stat = cMap["stat"] - a.cltCfg.Property = cMap["property"] - a.cltCfg.ModuleName = cMap["modulename"] - a.cltCfg.AsyncInvokeTimeout = c.GetIntWithDef("/tars/application/client", AsyncInvokeTimeout) - a.cltCfg.RefreshEndpointInterval = c.GetIntWithDef("/tars/application/client", refreshEndpointInterval) - a.cltCfg.ReportInterval = c.GetIntWithDef("/tars/application/client", reportInterval) - a.cltCfg.CheckStatusInterval = c.GetIntWithDef("/tars/application/client", checkStatusInterval) - a.cltCfg.KeepAliveInterval = c.GetIntWithDef("/tars/application/client", keepAliveInterval) - - // add client timeout - a.cltCfg.ClientQueueLen = c.GetIntWithDef("/tars/application/client", ClientQueueLen) - a.cltCfg.ClientIdleTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientIdleTimeout)) - a.cltCfg.ClientReadTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientReadTimeout)) - a.cltCfg.ClientWriteTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientWriteTimeout)) - a.cltCfg.ClientDialTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientDialTimeout)) - a.cltCfg.ReqDefaultTimeout = c.GetInt32WithDef("/tars/application/client", ReqDefaultTimeout) - a.cltCfg.ObjQueueMax = c.GetInt32WithDef("/tars/application/client", ObjQueueMax) - a.cltCfg.context["node_name"] = a.svrCfg.NodeName - ca := c.GetString("/tars/application/client") - if ca != "" { - cert := c.GetString("/tars/application/client") - key := c.GetString("/tars/application/client") - ciphers := c.GetString("/tars/application/client") - clientTlsConfig, err := ssl.NewClientTlsConfig(ca, cert, key, ciphers) - if err != nil { - panic(err) - } - a.clientTlsConfig = clientTlsConfig - } - serList := c.GetDomain("/tars/application/server") for _, adapter := range serList { endString := c.GetString("/tars/application/server/" + adapter + "") @@ -285,7 +260,7 @@ func (a *application) initConfig() { key := c.GetString("/tars/application/server/" + adapter + "") cert := c.GetString("/tars/application/server/" + adapter + "") if key != "" && cert != "" { - ca = c.GetString("/tars/application/server/" + adapter + "") + ca := c.GetString("/tars/application/server/" + adapter + "") verifyClient := c.GetString("/tars/application/server/"+adapter+"") != "0" ciphers := c.GetString("/tars/application/server/" + adapter + "") var adpTlsConfig *tls.Config @@ -303,8 +278,6 @@ func (a *application) initConfig() { } a.serList = serList - TLOG.Debug("config add ", a.tarsConfig) - if len(a.svrCfg.Local) > 0 { localPoint := endpoint.Parse(a.svrCfg.Local) // 管理端口不启动协程池 @@ -313,6 +286,43 @@ func (a *application) initConfig() { RegisterAdmin(rogger.Admin, rogger.HandleDyeingAdmin) } + TLOG.Debug("config add ", a.tarsConfig) +} + +func (a *application) parseClientConfig(c *conf.Conf) { + // init client config + cMap := c.GetMap("/tars/application/client") + a.cltCfg.Locator = cMap["locator"] + a.cltCfg.Stat = cMap["stat"] + a.cltCfg.Property = cMap["property"] + a.cltCfg.ModuleName = cMap["modulename"] + a.cltCfg.AsyncInvokeTimeout = c.GetIntWithDef("/tars/application/client", AsyncInvokeTimeout) + a.cltCfg.RefreshEndpointInterval = c.GetIntWithDef("/tars/application/client", refreshEndpointInterval) + a.cltCfg.ReportInterval = c.GetIntWithDef("/tars/application/client", reportInterval) + a.cltCfg.CheckStatusInterval = c.GetIntWithDef("/tars/application/client", checkStatusInterval) + a.cltCfg.KeepAliveInterval = c.GetIntWithDef("/tars/application/client", keepAliveInterval) + + // add client timeout + a.cltCfg.ClientQueueLen = c.GetIntWithDef("/tars/application/client", ClientQueueLen) + a.cltCfg.ClientIdleTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientIdleTimeout)) + a.cltCfg.ClientReadTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientReadTimeout)) + a.cltCfg.ClientWriteTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientWriteTimeout)) + a.cltCfg.ClientDialTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client", ClientDialTimeout)) + a.cltCfg.ReqDefaultTimeout = c.GetInt32WithDef("/tars/application/client", ReqDefaultTimeout) + a.cltCfg.ObjQueueMax = c.GetInt32WithDef("/tars/application/client", ObjQueueMax) + a.cltCfg.context["node_name"] = a.svrCfg.NodeName + ca := c.GetString("/tars/application/client") + if ca != "" { + cert := c.GetString("/tars/application/client") + key := c.GetString("/tars/application/client") + ciphers := c.GetString("/tars/application/client") + clientTlsConfig, err := ssl.NewClientTlsConfig(ca, cert, key, ciphers) + if err != nil { + panic(err) + } + a.clientTlsConfig = clientTlsConfig + } + auths := c.GetDomain("/tars/application/client") for _, objName := range auths { authInfo := make(map[string]string) @@ -324,15 +334,13 @@ func (a *application) initConfig() { authInfo["ciphers"] = c.GetString("/tars/application/client/" + objName + "") a.clientObjInfo[objName] = authInfo if authInfo["ca"] != "" { - var objTlsConfig *tls.Config - objTlsConfig, err = ssl.NewClientTlsConfig(authInfo["ca"], authInfo["cert"], authInfo["key"], authInfo["ciphers"]) + objTlsConfig, err := ssl.NewClientTlsConfig(authInfo["ca"], authInfo["cert"], authInfo["key"], authInfo["ciphers"]) if err != nil { panic(err) } a.clientObjTlsConfig[objName] = objTlsConfig } } - _, _ = maxprocs.Set(maxprocs.Logger(TLOG.Infof)) } // Run the application