This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
godog.go
279 lines (241 loc) · 7.76 KB
/
godog.go
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
/**
* Copyright 2018 gd Author. All Rights Reserved.
* Author: Chuck1024
*/
package gd
import (
"crypto/tls"
"fmt"
"github.com/gdp-org/gd/dlog"
"github.com/gdp-org/gd/net/dgrpc"
"github.com/gdp-org/gd/net/dhttp"
"github.com/gdp-org/gd/net/dogrpc"
"github.com/gdp-org/gd/runtime/helper"
"github.com/gdp-org/gd/runtime/inject"
"github.com/gdp-org/gd/runtime/pc"
"github.com/gdp-org/gd/runtime/stat"
"github.com/gdp-org/gd/utls"
"google.golang.org/grpc"
"os"
"runtime"
"syscall"
"time"
)
type Engine struct {
HttpServer *dhttp.HttpServer
RpcServer *dogrpc.RpcServer
GrpcServer *dgrpc.GrpcServer
}
func Default() *Engine {
e := &Engine{
HttpServer: &dhttp.HttpServer{},
RpcServer: dogrpc.NewDogRpcServer(),
GrpcServer: &dgrpc.GrpcServer{},
}
initLog()
inject.InitDefault()
inject.SetLogger(dlog.Global)
return e
}
func initLog() {
enable := Config("Log", "enable").MustBool(false)
if enable {
var port int
if Config("Server", "httpPort").MustInt() > 0 {
port = Config("Server", "httpPort").MustInt()
} else if Config("Server", "rpcPort").MustInt() > 0 {
port = Config("Server", "rpcPort").MustInt()
} else if Config("Server", "grpcPort").MustInt() > 0 {
port = Config("Server", "grpcPort").MustInt()
}
log := &gdConfig{
BinName: Config("Server", "serverName").String(),
Port: port,
LogLevel: Config("Log", "level").MustString(defaultFormat),
LogDir: Config("Log", "logDir").String(),
Stdout: Config("Log", "stdout").MustString("true"),
Format: Config("Log", "format").MustString(defaultFormat),
Rotate: Config("Log", "rotate").MustString("true"),
Maxsize: Config("Log", "maxsize").MustString("0M"),
MaxLines: Config("Log", "maxLines").MustString("0k"),
RotateType: Config("Log", "rotateType").MustString("hourly"),
}
if err := log.initLogConfig(); err != nil {
panic(fmt.Sprintf("initLogConfig occur error:%v", err))
}
}
}
// Engine Run
func (e *Engine) Run() error {
Info("- - - - - - - - - - - - - - - - - - -")
Info("process start")
// register signal
e.Signal()
defer inject.Close()
var err error
// dump when error occurs
logDir := Config("Log", "logDir").String()
file := &os.File{}
if Config("Log", "toFile").MustString("false") == "true" {
file, err = utls.Dump(logDir, Config("Server", "serverName").String())
if err != nil {
Error("Error occurs when initialize dump dumpPanic file, error = %s", err.Error())
}
}
// output exit info
defer func() {
Info("server stop...code: %d", runtime.NumGoroutine())
time.Sleep(time.Second)
Info("server stop...ok")
Info("- - - - - - - - - - - - - - - - - - -")
if Config("Log", "toFile").MustString("false") == "false" {
return
}
if err := utls.ReviewDumpPanic(file); err != nil {
Error("Failed to review dump dumpPanic file, error = %s", err.Error())
}
LogClose()
}()
// init cpu and memory
err = e.initCPUAndMemory()
if err != nil {
Error("Cannot init CPU and memory module, error = %s", err.Error())
return err
}
// init falcon
falconEnable := Config("Statistics", "falcon").MustBool(false)
if falconEnable {
pc.Init()
defer pc.ClosePerfCounter()
}
// init stat
statEnable := Config("Statistics", "stat").MustBool(false)
if statEnable {
statInterval := Config("Statistics", "statInterval").MustInt64(5)
statFile := "stat.log"
if Config("Log", "toFile").MustString("false") == "true" {
if logDir != "" {
statFile = logDir + "/stat.log"
}
}
stat.StatMgrInstance().Init(statFile, time.Second*time.Duration(statInterval))
}
// http server
httpPort := Config("Server", "httpPort").MustInt()
httpAddr := Config("Server", "httpAddr").MustString("")
if httpPort > 0 {
Info("http server try listen port:%d", httpPort)
inject.RegisterOrFail("httpServerRunPort", httpPort)
if httpAddr != "" {
Info("http server try listen addr:%d", httpAddr)
inject.RegisterOrFail("httpServerRunAddr", httpAddr)
}
inject.RegisterOrFail("httpServer", e.HttpServer)
if falconEnable {
pc.SetRunPort(httpPort)
}
}
// grpc server
grpcPort := Config("Server", "grpcPort").MustInt()
if grpcPort > 0 {
Info("grpc server try listen port:%d", grpcPort)
inject.RegisterOrFail("grpcRunHost", grpcPort)
inject.RegisterOrFail("serviceName", Config("Server", "serverName").String())
inject.RegisterOrFail("grpcServer", e.GrpcServer)
if falconEnable {
pc.SetRunPort(grpcPort)
}
}
// health
healthPort := Config("Process", "healthPort").MustInt()
if healthPort > 0 {
Info("health server try listen port:%d", healthPort)
inject.RegisterOrFail("helperHost", healthPort)
inject.RegisterOrFail("helper", (*helper.Helper)(nil))
}
// rpc server
rpcPort := Config("Server", "rpcPort").MustInt()
if rpcPort > 0 {
Info("rpc server try listen port:%d", rpcPort)
inject.RegisterOrFail("rpcHost", rpcPort)
inject.RegisterOrFail("rpcServer", e.RpcServer)
}
Close()
return nil
}
func (e *Engine) initCPUAndMemory() error {
maxCPU := Config("Process", "maxCPU").MustInt()
numCpus := runtime.NumCPU()
if maxCPU <= 0 {
if numCpus > 3 {
maxCPU = numCpus / 2
} else {
maxCPU = 1
}
} else if maxCPU > numCpus {
maxCPU = numCpus
}
runtime.GOMAXPROCS(maxCPU)
if Config("Process", "maxMemory").String() != "" {
maxMemory, err := utls.ParseMemorySize(Config("Process", "maxMemory").String())
if err != nil {
Crash(fmt.Sprintf("conf field illgeal, max_memory:%s, error:%s", Config("Process", "maxMemory").String(), err.Error()))
}
var rlimit syscall.Rlimit
syscall.Getrlimit(syscall.RLIMIT_AS, &rlimit)
Info("old rlimit mem:%v", rlimit)
rlimit.Cur = uint64(maxMemory)
rlimit.Max = uint64(maxMemory)
err = syscall.Setrlimit(syscall.RLIMIT_AS, &rlimit)
if err != nil {
Crash(fmt.Sprintf("syscall Setrlimit fail, rlimit:%v, error:%s", rlimit, err.Error()))
} else {
syscall.Getrlimit(syscall.RLIMIT_AS, &rlimit)
Info("new rlimit mem:%v", rlimit)
}
}
return nil
}
// default dog rpc client
func NewRpcClient(timeout time.Duration, retryNum uint32) *dogrpc.RpcClient {
client := NewRpcClientTls(timeout, retryNum, false)
return client
}
func NewRpcClientTls(timeout time.Duration, retryNum uint32, useTls bool) *dogrpc.RpcClient {
client := NewRpcClientTlsConfig(timeout, retryNum, useTls, nil)
return client
}
func NewRpcClientTlsConfig(timeout time.Duration, retryNum uint32, useTls bool, cfg *tls.Config) *dogrpc.RpcClient {
client := NewRpcClientTlsFromFile(timeout, retryNum, useTls, cfg, "", "", "")
return client
}
func NewRpcClientTlsFromFile(timeout time.Duration, retryNum uint32, useTls bool, cfg *tls.Config, ca, clientKey, clientPem string) *dogrpc.RpcClient {
client := dogrpc.NewClient(timeout, retryNum, useTls, cfg, ca, clientKey, clientPem)
return client
}
// default http client
func NewHttpClient() *dhttp.HttpClient {
return dhttp.New()
}
// default grpc client
func NewGrpcClient(target string, makeRawClient func(conn *grpc.ClientConn) (interface{}, error), serviceName string) *dgrpc.GrpcClient {
return NewGrpcClientTls(target, makeRawClient, serviceName, false)
}
func NewGrpcClientTls(target string, makeRawClient func(conn *grpc.ClientConn) (interface{}, error), serviceName string, useTls bool) *dgrpc.GrpcClient {
return NewGrpcClientTlsFromFile(target, makeRawClient, serviceName, useTls, "", "", "")
}
func NewGrpcClientTlsFromFile(target string, makeRawClient func(conn *grpc.ClientConn) (interface{}, error), serviceName string, useTls bool, ca, clientKey, clientPem string) *dgrpc.GrpcClient {
client := &dgrpc.GrpcClient{
Target: target,
ServiceName: serviceName,
UseTls: useTls,
GrpcCaPemFile: ca,
GrpcClientKeyFile: clientKey,
GrpcClientPemFile: clientPem,
}
if err := client.Start(makeRawClient); err != nil {
Error("grpc client start occur error:%s", err.Error())
return nil
}
return client
}