From 3c5dd4c8d0c5c5920b5b76edf14c08da955bc4bf Mon Sep 17 00:00:00 2001 From: lengcharles Date: Fri, 22 Dec 2023 12:37:31 +0800 Subject: [PATCH] style: reduce useless code and config (#129) * style(platform): change * style(platform): reduce useless code and config * style(platform): reduce useless code and config --- platform/manifest/config/config-example.yaml | 3 +- .../server/cmd/api/pkg/manager/manager.go | 2 - platform/server/shared/config/app/config.go | 13 +--- .../shared/config/internal/api/config.go | 11 ++- platform/server/shared/registry/builtin.go | 6 +- platform/server/template/agent/main_tpl.yaml | 69 ------------------- platform/server/template/api/layout.yaml | 16 ++--- platform/server/template/api/package.yaml | 22 +++--- 8 files changed, 32 insertions(+), 110 deletions(-) delete mode 100644 platform/server/template/agent/main_tpl.yaml diff --git a/platform/manifest/config/config-example.yaml b/platform/manifest/config/config-example.yaml index f7afdbe4..5bda2456 100644 --- a/platform/manifest/config/config-example.yaml +++ b/platform/manifest/config/config-example.yaml @@ -7,11 +7,10 @@ app: api: host: '0.0.0.0' # api service listening host port: 8089 # api service listening port - updateInterval: '3s' # interval that get service change from registry and push tasks change to agent services agent: addr: '0.0.0.0:11010' # agent service listening address - workerNum: 1 # worker num that process sync tasks. set to 0 to enable dynamic adjust worker num + workerNum: 1 # worker num that process sync tasks registry: builtin: diff --git a/platform/server/cmd/api/pkg/manager/manager.go b/platform/server/cmd/api/pkg/manager/manager.go index 1970e434..c6f4cc4d 100644 --- a/platform/server/cmd/api/pkg/manager/manager.go +++ b/platform/server/cmd/api/pkg/manager/manager.go @@ -51,7 +51,6 @@ type Manager struct { agents []*service.Service syncAgentServiceInterval time.Duration - syncRepositoryInterval time.Duration syncIdlInterval time.Duration daoManager *dao.Manager @@ -70,7 +69,6 @@ func NewManager(appConf app.Config, daoManager *dao.Manager, dispatcher dispatch agents: make([]*service.Service, 0), syncAgentServiceInterval: appConf.GetSyncAgentServiceInterval(), - syncRepositoryInterval: appConf.GetSyncRepositoryInterval(), syncIdlInterval: appConf.GetSyncIdlInterval(), daoManager: daoManager, diff --git a/platform/server/shared/config/app/config.go b/platform/server/shared/config/app/config.go index a3b82646..34001758 100644 --- a/platform/server/shared/config/app/config.go +++ b/platform/server/shared/config/app/config.go @@ -29,13 +29,11 @@ type Config struct { Timezone string `mapstructure:"timezone"` ProxyUrl string `mapstructure:"proxyUrl"` SyncAgentServiceInterval string `mapstructure:"syncAgentServiceInterval"` - SyncRepositoryInterval string `mapstructure:"syncRepositoryInterval"` SyncIdlInterval string `mapstructure:"syncIdlInterval"` } const ( - defaultSyncAgentServiceInterval = 3 * time.Second - defaultSyncRepositoryInterval = 3 * time.Minute + defaultSyncAgentServiceInterval = 10 * time.Second defaultSyncIdlInterval = 3 * time.Minute ) @@ -56,15 +54,6 @@ func (conf *Config) GetSyncAgentServiceInterval() time.Duration { return defaultSyncAgentServiceInterval } -func (conf *Config) GetSyncRepositoryInterval() time.Duration { - duration, err := time.ParseDuration(conf.SyncIdlInterval) - if err == nil { - return duration - } - - return defaultSyncRepositoryInterval -} - func (conf *Config) GetSyncIdlInterval() time.Duration { duration, err := time.ParseDuration(conf.SyncIdlInterval) if err == nil { diff --git a/platform/server/shared/config/internal/api/config.go b/platform/server/shared/config/internal/api/config.go index d8d4c066..147d94ed 100644 --- a/platform/server/shared/config/internal/api/config.go +++ b/platform/server/shared/config/internal/api/config.go @@ -23,12 +23,11 @@ import ( ) type Config struct { - Host string `mapstructure:"host"` - Port int `mapstructure:"port"` - Tracing TracerConf `mapstructure:"tracing"` - MetricsUrl string `mapstructure:"metricsUrl"` - Dispatcher dispatcher.Config `mapstructure:"dispatcher"` - UpdateInterval string `mapstructure:"updateInterval"` + Host string `mapstructure:"host"` + Port int `mapstructure:"port"` + Tracing TracerConf `mapstructure:"tracing"` + MetricsUrl string `mapstructure:"metricsUrl"` + Dispatcher dispatcher.Config `mapstructure:"dispatcher"` } type TracerConf struct { diff --git a/platform/server/shared/registry/builtin.go b/platform/server/shared/registry/builtin.go index 6a529cbe..50e3bd53 100644 --- a/platform/server/shared/registry/builtin.go +++ b/platform/server/shared/registry/builtin.go @@ -349,6 +349,10 @@ type BuiltinKitexRegistryClient struct { updateInterval time.Duration } +const ( + defaultClientUpdateInterval = 10 * time.Second +) + func NewBuiltinKitexRegistryClient(addr string) (*BuiltinKitexRegistryClient, error) { httpRes, err := http.Get(fmt.Sprintf("http://%s/api/ping", addr)) if err != nil { @@ -375,7 +379,7 @@ func NewBuiltinKitexRegistryClient(addr string) (*BuiltinKitexRegistryClient, er return &BuiltinKitexRegistryClient{ addr: addr, stopChan: make(chan struct{}), - updateInterval: 10 * time.Second, + updateInterval: defaultClientUpdateInterval, }, nil } diff --git a/platform/server/template/agent/main_tpl.yaml b/platform/server/template/agent/main_tpl.yaml deleted file mode 100644 index 0c98bf53..00000000 --- a/platform/server/template/agent/main_tpl.yaml +++ /dev/null @@ -1,69 +0,0 @@ -path: main.go -update_behavior: - type: cover -body: |- - /* - * - * Copyright 2022 CloudWeGo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - - package main - - import ( - "fmt" - apollo "wecqupt/app/common/config" - "wecqupt/app/common/config/model" - "{{.ImportPath}}/{{ToLower .ServiceName}}" - "wecqupt/app/common/logger" - "{{.Module}}/internal/config" - "{{.Module}}/internal/svc" - "wecqupt/utils" - - "go.uber.org/zap" - ) - - var c config.Config - - func main() { - // 初始化配置管理器 - err := apollo.InitClient(config.ServiceName) - if err != nil { - panic(fmt.Sprintf("initialize Apollo Client failed, err: %v", err)) - } - - namespace, serviceSingleName := utils.GetServiceDetails(config.ServiceName) - - err = apollo.Common().UnmarshalServiceConfig(namespace, serviceSingleName, &c) - - // 初始化服务 - c.SetUp() - - // 初始化服务上下文 - svcCtx := svc.NewServiceContext(c) - - // 启动服务 - svr := {{ToLower .ServiceName}}.NewServer( - &{{.ServiceName}}Impl{ - svcCtx: svcCtx, - }, - model.KitexServerOptions..., - ) - - err = svr.Run() - if err != nil { - logger.Logger.Error("kitex server run failed.", zap.Error(err)) - } - } \ No newline at end of file diff --git a/platform/server/template/api/layout.yaml b/platform/server/template/api/layout.yaml index 27ba2f2b..a57f05b7 100644 --- a/platform/server/template/api/layout.yaml +++ b/platform/server/template/api/layout.yaml @@ -1,24 +1,21 @@ layouts: - # 生成的 handler 的目录, 只有目录下有文件才会生成 - path: internal/biz/handler/ delims: - "" - "" body: "" - # 生成的 model 的目录, 只有目录下有文件才会生成 - path: internal/biz/model/ delims: - "" - "" body: "" - # 生成的 svc 的目录, 只有目录下有文件才会生成 - path: internal/svc/ delims: - "" - "" body: "" - # .hz 文件,包含 hz 版本,是 hz 创建的项目的标志,不需要传渲染数据 + # .hz file,contains hz version - path: .hz delims: - '{{' @@ -27,7 +24,6 @@ layouts: // Code generated by hz. DO NOT EDIT. hz version: {{.hzVersion}} - # ping 自带 ping 的 handler - path: internal/biz/handler/ping.go delims: - "" @@ -66,7 +62,7 @@ layouts: "message": "pong", }) } - # 自定义路由注册的文件 + # customize router file - path: router.go delims: - "" @@ -103,7 +99,7 @@ layouts: // your code ... } - # 定义路由注册的文件,需要模板渲染数据{{.RouterPkgPath}}才能生成 + # router file, need template render data{{.RouterPkgPath}} to generate - path: router_gen.go delims: - "" @@ -141,7 +137,7 @@ layouts: customizedRegister(r) } - # 默认中间件文件,不要修改 + # default middleware file - path: internal/biz/router/middleware.go delims: - "" @@ -192,7 +188,7 @@ layouts: return []app.HandlerFunc{} } - # 默认路由注册文件,不要修改 + # default router register file - path: internal/biz/router/register.go delims: - "" @@ -228,7 +224,7 @@ layouts: g := r.Group("", rootMw()...) } - # 服务上下文文件 + # service context file - path: internal/svc/servicecontext.go delims: - "" diff --git a/platform/server/template/api/package.yaml b/platform/server/template/api/package.yaml index 021dd556..76b5d82a 100644 --- a/platform/server/template/api/package.yaml +++ b/platform/server/template/api/package.yaml @@ -1,6 +1,6 @@ -# 以下数据都是 yaml marshal 得到的,所以可能看起来比较乱 layouts: - # path 只表示 handler.go 的模板,具体的 handler 路径由默认路径和 handler_dir 决定 + # path only represent it's a template of handler.go + # handler file path is decided by handler_dir - path: handler.go delims: - '{{' @@ -76,7 +76,8 @@ layouts: response.OkWithData(rCtx, res.Msg, res.Data) } {{end}} - # handler_single 表示单独的 handler 模板,用于 update 的时候更新每一个新增的 handler + # handler_single only represent it's a single handler template + # it's used to update every handler when use hz update - path: handler_single.go delims: - '{{' @@ -175,7 +176,8 @@ layouts: Msg: successMsg{{.HttpMethod.Name}}, } } - # path 只表示 router.go 的模板,其路径固定在:biz/router/namespace/ + # path represent it's a template of router.go + # the file path is fixed in biz/router/namespace/ - path: router.go delims: - '{{' @@ -239,7 +241,8 @@ layouts: func Register(r *route.RouterGroup) { {{template "G" .Router}} } - # path 只表示 register.go 的模板,register 的路径固定为 biz/router/register.go + # path represent it's a template of register.go + # the file path is fixed in biz/router/register.go - path: register.go delims: - "" @@ -280,7 +283,8 @@ layouts: - "" - "" body: "" - # path 只表示 middleware.go 的模板,middleware 的路径和 router.go 一样为:biz/router/namespace/ + # path only represent it's a template of middleware.go + # the path of middleware and router.go is in biz/router/namespace/ - path: middleware.go delims: - '{{' @@ -319,7 +323,8 @@ layouts: {{- end}} {{template "M" .Router}} - # middleware_single 表示单独的 middleware 模板,用于 update 的时候更新每一个新增的 middleware_single + # middleware_single represent it's a template of single middleware + # it will update every middleware_single when use hz update - path: middleware_single.go delims: - '{{' @@ -329,7 +334,8 @@ layouts: // your code... return nil } - # path 只表示 client.go 的模板,client 代码的生成路径由用户指定"${client_dir}" + # path only represent it's a template of client.go + # client file generate path is specified by ${client_dir}" - path: client.go delims: - '{{'