-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.go
More file actions
320 lines (268 loc) · 9.16 KB
/
app.go
File metadata and controls
320 lines (268 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* This file is part of Golaxy Distributed Service Development Framework.
*
* Golaxy Distributed Service Development Framework is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Golaxy Distributed Service Development Framework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Golaxy Distributed Service Development Framework. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2024 pangdogs.
*/
package framework
import (
"context"
"errors"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"git.golaxy.org/core"
"git.golaxy.org/core/utils/exception"
"git.golaxy.org/core/utils/generic"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)
// NewApp 创建应用
func NewApp() *App {
app := &App{
conf: viper.New(),
}
app.cmd = &cobra.Command{
Short: "Application for Launching Services",
Run: func(*cobra.Command, []string) {
// 加载参数配置
app.initConf()
// 加载pprof
app.initPProf()
// 执行启动回调
app.startingCB.UnsafeCall(app)
// 主循环
app.mainLoop()
// 执行结束回调
app.terminatedCB.UnsafeCall(app)
},
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}
return app
}
type _SupportedService struct {
assembler iServiceAssembler
count int
}
// App 应用
type App struct {
services generic.SliceMap[string, *_SupportedService]
conf *viper.Viper
cmd *cobra.Command
initCB, startingCB, terminatedCB generic.Action1[*App]
initOnce bool
}
// SetAssembler 设置服务实例装配器(传入实例类型时,将会自动创建装配器)
func (app *App) SetAssembler(name string, assembler any) *App {
if app.conf == nil {
exception.Panicf("%w: conf is nil", ErrFramework)
}
if app.cmd == nil {
exception.Panicf("%w: cmd is nil", ErrFramework)
}
if assembler == nil {
exception.Panicf("%w: %w: assembler is nil", ErrFramework, core.ErrArgs)
}
assemblerInst, ok := assembler.(iServiceAssembler)
if !ok {
assemblerInst = newServiceInstantiator(assembler)
}
assemblerInst.init(app.conf, app.cmd, name, assemblerInst)
app.services.Add(name,
&_SupportedService{
assembler: assemblerInst,
count: 1,
},
)
return app
}
// InitCB 初始化回调(可以用于自定义启动参数)
func (app *App) InitCB(cb generic.Action1[*App]) *App {
app.initCB = cb
return app
}
// StartingCB 启动回调
func (app *App) StartingCB(cb generic.Action1[*App]) *App {
app.startingCB = cb
return app
}
// TerminateCB 终止回调
func (app *App) TerminateCB(cb generic.Action1[*App]) *App {
app.terminatedCB = cb
return app
}
// Run 运行
func (app *App) Run() {
if app.conf == nil {
exception.Panicf("%w: conf is nil", ErrFramework)
}
if app.cmd == nil {
exception.Panicf("%w: cmd is nil", ErrFramework)
}
if !app.initOnce {
app.initOnce = true
// 初始化启动参数
app.initFlags()
// 执行初始化回调
app.initCB.UnsafeCall(app)
}
// 开始运行
if err := app.cmd.Execute(); err != nil {
exception.Panicf("%w: %w", ErrFramework, err)
}
}
// Conf 获取参数配置
func (app *App) Conf() *viper.Viper {
return app.conf
}
// Cmd 获取启动命令
func (app *App) Cmd() *cobra.Command {
if app.conf == nil {
exception.Panicf("%w: conf is nil", ErrFramework)
}
if app.cmd == nil {
exception.Panicf("%w: cmd is nil", ErrFramework)
}
// 执行初始化回调
if !app.initOnce {
app.initOnce = true
// 初始化启动参数
app.initFlags()
// 执行初始化回调
app.initCB.UnsafeCall(app)
}
return app.cmd
}
func (app *App) initFlags() {
cmd := app.cmd
// 日志参数
cmd.PersistentFlags().String("log.level", zap.InfoLevel.String(), "log level: [debug|info|warn|error|dpanic|panic|fatal]")
cmd.PersistentFlags().String("log.encoder", "development", "log encoder: [production|development]")
cmd.PersistentFlags().String("log.format", "console", "log format: [console|json]")
cmd.PersistentFlags().Bool("log.async", true, "enable async log writer")
cmd.PersistentFlags().Int("log.buffer_size", 512*1024, "async log buffer size in bytes")
cmd.PersistentFlags().Duration("log.flush_interval", time.Second, "async log flush interval, e.g. 1s")
// 配置参数
cmd.PersistentFlags().String("conf.env_prefix", "", "defines the prefix for environment variables")
cmd.PersistentFlags().String("conf.local_path", "", "local config file path")
cmd.PersistentFlags().String("conf.remote_provider", "", "remote config provider")
cmd.PersistentFlags().String("conf.remote_endpoint", "", "remote config endpoint")
cmd.PersistentFlags().String("conf.remote_path", "", "remote config file path")
// nats参数
cmd.PersistentFlags().String("nats.address", "localhost:4222", "nats address")
cmd.PersistentFlags().String("nats.username", "", "nats auth username")
cmd.PersistentFlags().String("nats.password", "", "nats auth password")
// etcd参数
cmd.PersistentFlags().String("etcd.address", "localhost:2379", "etcd address")
cmd.PersistentFlags().String("etcd.username", "", "etcd auth username")
cmd.PersistentFlags().String("etcd.password", "", "etcd auth password")
// 分布式服务参数
cmd.PersistentFlags().String("service.version", "v0.0.0", "service version info")
cmd.PersistentFlags().StringToString("service.meta", map[string]string{}, "service meta info")
cmd.PersistentFlags().Duration("service.ttl", 10*time.Second, "ttl for service keepalive")
cmd.PersistentFlags().Duration("service.future_timeout", 3*time.Second, "timeout for future model of service interaction")
cmd.PersistentFlags().Duration("service.dent_ttl", 10*time.Second, "ttl for distributed entity keepalive")
cmd.PersistentFlags().Bool("service.auto_recover", false, "enable panic auto recover")
// 启动的服务列表
cmd.PersistentFlags().StringToString("startup.services", func() map[string]string {
ret := map[string]string{}
app.services.Each(func(name string, service *_SupportedService) {
ret[name] = strconv.Itoa(service.count)
})
return ret
}(), "instances required for each service to start")
// pprof参数
cmd.PersistentFlags().Bool("pprof.enable", false, "enable pprof")
cmd.PersistentFlags().String("pprof.address", "0.0.0.0:6060", "pprof listening address")
}
func (app *App) initConf() {
conf := app.conf
// 合并启动参数
conf.BindPFlags(app.cmd.Flags())
// 合并环境变量
conf.SetEnvPrefix(conf.GetString("conf.env_prefix"))
conf.AutomaticEnv()
// 加载本地配置文件
localPath := conf.GetString("conf.local_path")
if localPath != "" {
conf.SetConfigFile(localPath)
if err := conf.ReadInConfig(); err != nil {
exception.Panicf("%w: read local config failed, path:%q, %s", ErrFramework, localPath, err)
}
}
// 加载远程配置文件
remoteProvider := conf.GetString("conf.remote_provider")
remoteEndpoint := conf.GetString("conf.remote_endpoint")
remotePath := conf.GetString("conf.remote_path")
if remoteProvider != "" {
if err := conf.AddRemoteProvider(remoteProvider, remoteEndpoint, remotePath); err != nil {
exception.Panicf(`%w: set remote config failed, provider:%q, endpoint:%q, path:%q, %s`, ErrFramework, remoteProvider, remoteEndpoint, remotePath, err)
}
if err := conf.ReadRemoteConfig(); err != nil {
exception.Panicf(`%w: read remote config failed, provider:%q, endpoint:%q, path:%q, %s`, ErrFramework, remoteProvider, remoteEndpoint, remotePath, err)
}
}
}
func (app *App) initPProf() {
if !app.Conf().GetBool("pprof.enable") {
return
}
addr := app.Conf().GetString("pprof.address")
_, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
exception.Panicf("%w: invalid pprof address %q, %s", ErrFramework, addr, err)
}
go func() {
if err := http.ListenAndServe(addr, nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
exception.Panicf("%w: interrupt listening %q, %s", ErrFramework, addr, err)
}
}()
}
func (app *App) mainLoop() {
// 监听退出信号
ctx, cancel := context.WithCancel(context.Background())
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-sigChan
cancel()
}()
// 启动所有服务
wg := &sync.WaitGroup{}
bootstrap := app.conf.GetStringMapString("startup.services")
app.services.Each(func(name string, service *_SupportedService) {
service.count, _ = strconv.Atoi(bootstrap[name])
})
app.services.Each(func(name string, service *_SupportedService) {
for i := 0; i < service.count; i++ {
wg.Add(1)
go func(assembler iServiceAssembler, replicaNo int) {
defer wg.Done()
<-assembler.assemble(ctx, replicaNo).Run().Done()
}(service.assembler, i)
}
})
// 等待运行结束
wg.Wait()
}