/
entry.go
380 lines (306 loc) · 8.98 KB
/
entry.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package golug_grpc
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/gofiber/fiber/v2"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/pubgo/golug/golug_app"
"github.com/pubgo/golug/golug_entry/golug_base"
registry "github.com/pubgo/golug/golug_registry"
"github.com/pubgo/golug/pkg/golug_utils/addr"
"github.com/pubgo/xerror"
"github.com/pubgo/xlog"
"github.com/pubgo/xprocess"
"github.com/spf13/pflag"
"go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/channelz/service"
"google.golang.org/grpc/reflection"
)
var _ Entry = (*grpcEntry)(nil)
type grpcEntry struct {
*golug_base.Entry
exit chan chan error
name string
mu sync.RWMutex
cfg Cfg
registered atomic.Bool
srv *grpc.Server
registryMap map[string][]*registry.Endpoint
handlers []interface{}
endpoints []*registry.Endpoint
opts []grpc.ServerOption
unaryServerInterceptors []grpc.UnaryServerInterceptor
streamServerInterceptors []grpc.StreamServerInterceptor
}
func (g *grpcEntry) InitOpts(opts ...grpc.ServerOption) { g.opts = append(g.opts, opts...) }
// EnableDebug
// https://github.com/grpc/grpc-experiments/tree/master/gdebug
func (g *grpcEntry) EnableDebug() {
grpc.EnableTracing = true
reflection.Register(g.srv)
service.RegisterChannelzServiceToServer(g.srv)
}
func (g *grpcEntry) GetDefaultServerOpts() []grpc.ServerOption {
return []grpc.ServerOption{
grpc.MaxRecvMsgSize(g.cfg.MaxReceiveMessageSize),
grpc.MaxSendMsgSize(g.cfg.MaxSendMessageSize),
}
}
func (g *grpcEntry) Init() (gErr error) {
defer xerror.RespErr(&gErr)
xerror.Panic(g.Entry.Init())
unaryInterceptorList := unaryInterceptors
unaryInterceptorList = append(unaryInterceptorList, g.unaryServerInterceptors...)
streamInterceptorList := streamInterceptors
streamInterceptorList = append(streamInterceptorList, g.streamServerInterceptors...)
opts := GetDefaultServerOpts()
opts = append(opts, g.GetDefaultServerOpts()...)
opts = append(opts,
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptorList...)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamInterceptorList...)))
// 注册中心校验
g.cfg.registry = registry.Default
g.srv = grpc.NewServer(opts...)
return
}
func (g *grpcEntry) register() (err error) {
defer xerror.RespErr(&err)
if g.cfg.registry == nil {
return nil
}
// parse address for host, port
var advt, host string
var port int
// check the advertise address first
// if it exists then use it, otherwise
// use the address
if len(g.cfg.Advertise) > 0 {
advt = g.cfg.Advertise
} else {
advt = g.cfg.Address
}
parts := strings.Split(advt, ":")
if len(parts) > 1 {
host = strings.Join(parts[:len(parts)-1], ":")
port, _ = strconv.Atoi(parts[len(parts)-1])
} else {
host = parts[0]
}
addr1, err := addr.Extract(host)
if err != nil {
return xerror.WrapF(err, "addr Extract error, host:%s", host)
}
// register service
node := ®istry.Node{
Id: golug_app.Project + "-" + getHostname() + "-" + DefaultId,
Address: addr1,
Port: port,
}
node.Metadata["registry"] = g.cfg.registry.String()
node.Metadata["transport"] = "grpc"
services := ®istry.Service{
Name: golug_app.Project,
Version: g.Entry.Options().Version,
Nodes: []*registry.Node{node},
Endpoints: g.endpoints,
}
if !g.registered.Load() {
xlog.Infof("Registering node: %s", node.Id)
}
// create registry options
rOpts := []registry.RegisterOption{registry.TTL(g.cfg.RegisterTTL)}
if err := g.cfg.registry.Register(services, rOpts...); err != nil {
return xerror.WrapF(err, "[grpc] registry register error")
}
// already registered? don't need to register subscribers
if g.registered.Load() {
return nil
}
g.registered.Store(true)
return nil
}
func (g *grpcEntry) deregister() (err error) {
defer xerror.RespErr(&err)
if g.cfg.registry == nil {
return nil
}
var advt, host string
var port int
// check the advertise address first
// if it exists then use it, otherwise
// use the address
if len(g.cfg.Advertise) > 0 {
advt = g.cfg.Advertise
} else {
advt = g.cfg.Address
}
parts := strings.Split(advt, ":")
if len(parts) > 1 {
host = strings.Join(parts[:len(parts)-1], ":")
port, _ = strconv.Atoi(parts[len(parts)-1])
} else {
host = parts[0]
}
addr1, err := addr.Extract(host)
if err != nil {
return xerror.WrapF(err, "addr Extract error, host:%s", host)
}
node := ®istry.Node{
Id: golug_app.Project + "-" + getHostname() + "-" + DefaultId,
Address: addr1,
Port: port,
}
services := ®istry.Service{
Name: golug_app.Project,
Version: g.Entry.Options().Version,
Nodes: []*registry.Node{node},
}
xlog.Infof("DeRegistering node: %s", node.Id)
if err := g.cfg.registry.Deregister(services); err != nil {
return xerror.WrapF(err, "[grpc] registry deregister error")
}
if !g.registered.Load() {
return nil
}
g.registered.Store(false)
return nil
}
func (g *grpcEntry) Stop() (err error) {
defer xerror.RespErr(&err)
ch := make(chan error)
xlog.Info("[ExitProgress] Stop is called, send error chan. before.")
g.exit <- ch
xlog.Info("[ExitProgress] Stop is called, send error chan. end.")
return <-ch
}
func (g *grpcEntry) RegisterUnaryInterceptor(interceptors ...grpc.UnaryServerInterceptor) {
g.mu.Lock()
defer g.mu.Unlock()
g.unaryServerInterceptors = append(g.unaryServerInterceptors, interceptors...)
}
func (g *grpcEntry) RegisterStreamInterceptor(interceptors ...grpc.StreamServerInterceptor) {
g.mu.Lock()
defer g.mu.Unlock()
g.streamServerInterceptors = append(g.streamServerInterceptors, interceptors...)
}
func (g *grpcEntry) Register(handler interface{}, opts ...Option) {
xerror.Assert(handler == nil, "[handler] should not be nil")
xerror.ExitF(checkHandle(handler), "[grpc] grpcEntry.Register error")
g.mu.Lock()
defer g.mu.Unlock()
g.handlers = append(g.handlers, handler)
}
// 开启api网关模式
func (g *grpcEntry) startGw() (err error) {
if g.cfg.GwAddr == "" {
return nil
}
app := fiber.New()
// 开启api网关模式
if err := registerGw(
fmt.Sprintf("localhost:%d", g.Entry.Options().Port),
app.Group("/")); err != nil {
return err
}
var data []map[string]string
for i, stacks := range app.Stack() {
data = append(data, make(map[string]string))
for _, stack := range stacks {
if stack == nil {
continue
}
if stack.Path == "/" {
continue
}
data[i][stack.Method] = stack.Path
}
}
fmt.Printf("%#v\n", data)
return app.Listen(g.cfg.GwAddr)
}
func (g *grpcEntry) Start() (err error) {
defer xerror.RespErr(&err)
g.endpoints = g.endpoints[:0]
// 初始化routes
for i := range g.handlers {
g.endpoints = append(g.endpoints, newRpcHandler(g.handlers[i])...)
xerror.PanicF(register(g.srv, g.handlers[i]), "[grpc] register error")
}
//if g.cfg.Address == "" {
// return xerror.New("[grpc] please set address")
//}
ts, err := net.Listen("tcp", g.cfg.Address)
if err != nil {
return xerror.WrapF(err, "net Listen error, addr:%s", g.cfg.Address)
}
xlog.Infof("Server [grpc] Listening on %s", ts.Addr().String())
g.mu.Lock()
g.cfg.Address = ts.Addr().String()
g.mu.Unlock()
// announce self to the world
if err := g.register(); err != nil {
return xerror.WrapF(err, "[grpc] registry try register error")
}
xprocess.Go(func(ctx context.Context) {
if err := g.srv.Serve(ts); err != nil {
xlog.Errorf("[grpc] server stop error: %#v", err)
}
})
t := new(time.Ticker)
// only process if it exists
if g.cfg.RegisterInterval > time.Duration(0) {
// new ticker
t = time.NewTicker(g.cfg.RegisterInterval)
}
// return error chan
var ch chan error
Loop:
for {
select {
// register self on interval
case <-t.C:
if err := g.register(); err != nil {
xlog.Info("[grpc] server register error", xlog.Any("err", err))
}
// wait for exit
case ch = <-g.exit:
break Loop
}
}
// deregister self
if err := g.deregister(); err != nil {
xlog.Info("[grpc] server deregister error", xlog.Any("err", err))
}
// Add sleep for those requests which have selected this port.
time.Sleep(g.cfg.SleepAfterDeregister)
// wait for waitgroup
xlog.Info("[ExitProgress] Start wait-group wait.")
// stop the grpc server
xlog.Info("[ExitProgress] Start GracefulStop.")
g.srv.GracefulStop()
// close transport
xlog.Info("[ExitProgress] Close transport.")
ch <- nil
xlog.Info("[ExitProgress] All is done.")
return nil
}
func (g *grpcEntry) initFlags() {
g.Flags(func(flags *pflag.FlagSet) {
flags.StringVar(&g.cfg.GwAddr, "gw", g.cfg.GwAddr, "set addr and enable gateway mode")
})
}
func newEntry(name string) *grpcEntry {
ent := &grpcEntry{Entry: golug_base.New(name)}
ent.initFlags()
// 服务启动后, 启动网关
ent.AfterStart(func() { xerror.Panic(ent.startGw()) })
ent.OnCfgWithName(Name, &ent.cfg)
return ent
}
func New(name string) Entry { return newEntry(name) }