Skip to content

Commit

Permalink
Merge pull request #507 from TarsCloud/lbbniu/perf/config
Browse files Browse the repository at this point in the history
perf: optimize service configuration file parse
  • Loading branch information
lbbniu committed Jan 11, 2024
2 parents f4adeb5 + cf84518 commit f55dc1d
Showing 1 changed file with 70 additions and 62 deletions.
132 changes: 70 additions & 62 deletions tars/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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")
Expand All @@ -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<enableset>"), "Y") {
a.svrCfg.Enableset = true
Expand Down Expand Up @@ -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>", AcceptTimeout))
a.svrCfg.ReadTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/server<readtimeout>", ReadTimeout))
Expand All @@ -214,11 +219,14 @@ func (a *application) initConfig() {
a.svrCfg.StatReportChannelBufLen = c.GetInt32WithDef("/tars/application/server<statreportchannelbuflen>", StatReportChannelBufLen)
// maxPackageLength
a.svrCfg.MaxPackageLength = c.GetIntWithDef("/tars/application/server<maxPackageLength>", MaxPackageLength)
protocol.SetMaxPackageLength(a.svrCfg.MaxPackageLength)

// tls
a.svrCfg.Key = c.GetString("/tars/application/server<key>")
a.svrCfg.Cert = c.GetString("/tars/application/server<cert>")
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<ca>")
a.svrCfg.VerifyClient = c.GetStringWithDef("/tars/application/server<verifyclient>", "0") != "0"
Expand All @@ -233,39 +241,6 @@ func (a *application) initConfig() {
a.svrCfg.SampleAddress = c.GetString("/tars/application/server<sampleaddress>")
a.svrCfg.SampleEncoding = c.GetStringWithDef("/tars/application/server<sampleencoding>", "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<async-invoke-timeout>", AsyncInvokeTimeout)
a.cltCfg.RefreshEndpointInterval = c.GetIntWithDef("/tars/application/client<refresh-endpoint-interval>", refreshEndpointInterval)
a.cltCfg.ReportInterval = c.GetIntWithDef("/tars/application/client<report-interval>", reportInterval)
a.cltCfg.CheckStatusInterval = c.GetIntWithDef("/tars/application/client<check-status-interval>", checkStatusInterval)
a.cltCfg.KeepAliveInterval = c.GetIntWithDef("/tars/application/client<keep-alive-interval>", keepAliveInterval)

// add client timeout
a.cltCfg.ClientQueueLen = c.GetIntWithDef("/tars/application/client<clientqueuelen>", ClientQueueLen)
a.cltCfg.ClientIdleTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientidletimeout>", ClientIdleTimeout))
a.cltCfg.ClientReadTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientreadtimeout>", ClientReadTimeout))
a.cltCfg.ClientWriteTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientwritetimeout>", ClientWriteTimeout))
a.cltCfg.ClientDialTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientdialtimeout>", ClientDialTimeout))
a.cltCfg.ReqDefaultTimeout = c.GetInt32WithDef("/tars/application/client<reqdefaulttimeout>", ReqDefaultTimeout)
a.cltCfg.ObjQueueMax = c.GetInt32WithDef("/tars/application/client<objqueuemax>", ObjQueueMax)
a.cltCfg.context["node_name"] = a.svrCfg.NodeName
ca := c.GetString("/tars/application/client<ca>")
if ca != "" {
cert := c.GetString("/tars/application/client<cert>")
key := c.GetString("/tars/application/client<key>")
ciphers := c.GetString("/tars/application/client<ciphers>")
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 + "<endpoint>")
Expand All @@ -285,7 +260,7 @@ func (a *application) initConfig() {
key := c.GetString("/tars/application/server/" + adapter + "<key>")
cert := c.GetString("/tars/application/server/" + adapter + "<cert>")
if key != "" && cert != "" {
ca = c.GetString("/tars/application/server/" + adapter + "<ca>")
ca := c.GetString("/tars/application/server/" + adapter + "<ca>")
verifyClient := c.GetString("/tars/application/server/"+adapter+"<verifyclient>") != "0"
ciphers := c.GetString("/tars/application/server/" + adapter + "<ciphers>")
var adpTlsConfig *tls.Config
Expand All @@ -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)
// 管理端口不启动协程池
Expand All @@ -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<async-invoke-timeout>", AsyncInvokeTimeout)
a.cltCfg.RefreshEndpointInterval = c.GetIntWithDef("/tars/application/client<refresh-endpoint-interval>", refreshEndpointInterval)
a.cltCfg.ReportInterval = c.GetIntWithDef("/tars/application/client<report-interval>", reportInterval)
a.cltCfg.CheckStatusInterval = c.GetIntWithDef("/tars/application/client<check-status-interval>", checkStatusInterval)
a.cltCfg.KeepAliveInterval = c.GetIntWithDef("/tars/application/client<keep-alive-interval>", keepAliveInterval)

// add client timeout
a.cltCfg.ClientQueueLen = c.GetIntWithDef("/tars/application/client<clientqueuelen>", ClientQueueLen)
a.cltCfg.ClientIdleTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientidletimeout>", ClientIdleTimeout))
a.cltCfg.ClientReadTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientreadtimeout>", ClientReadTimeout))
a.cltCfg.ClientWriteTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientwritetimeout>", ClientWriteTimeout))
a.cltCfg.ClientDialTimeout = tools.ParseTimeOut(c.GetIntWithDef("/tars/application/client<clientdialtimeout>", ClientDialTimeout))
a.cltCfg.ReqDefaultTimeout = c.GetInt32WithDef("/tars/application/client<reqdefaulttimeout>", ReqDefaultTimeout)
a.cltCfg.ObjQueueMax = c.GetInt32WithDef("/tars/application/client<objqueuemax>", ObjQueueMax)
a.cltCfg.context["node_name"] = a.svrCfg.NodeName
ca := c.GetString("/tars/application/client<ca>")
if ca != "" {
cert := c.GetString("/tars/application/client<cert>")
key := c.GetString("/tars/application/client<key>")
ciphers := c.GetString("/tars/application/client<ciphers>")
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)
Expand All @@ -324,15 +334,13 @@ func (a *application) initConfig() {
authInfo["ciphers"] = c.GetString("/tars/application/client/" + objName + "<ciphers>")
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
Expand Down

0 comments on commit f55dc1d

Please sign in to comment.