forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trpc_util.go
432 lines (386 loc) · 11.9 KB
/
trpc_util.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package trpc
import (
"context"
"net"
"os"
"runtime"
"sync"
"time"
"github.com/panjf2000/ants/v2"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/errs"
"trpc.group/trpc-go/trpc-go/internal/report"
"trpc.group/trpc-go/trpc-go/log"
)
// PanicBufLen is len of buffer used for stack trace logging
// when the goroutine panics, 1024 by default.
var PanicBufLen = 1024
// ----------------------- trpc util functions ------------------------------------ //
// Message returns msg from ctx.
func Message(ctx context.Context) codec.Msg {
return codec.Message(ctx)
}
// BackgroundContext puts an initialized msg into background context and returns it.
func BackgroundContext() context.Context {
cfg := GlobalConfig()
ctx, msg := codec.WithNewMessage(context.Background())
msg.WithCalleeContainerName(cfg.Global.ContainerName)
msg.WithNamespace(cfg.Global.Namespace)
msg.WithEnvName(cfg.Global.EnvName)
if cfg.Global.EnableSet == "Y" {
msg.WithSetName(cfg.Global.FullSetName)
}
if len(cfg.Server.Service) > 0 {
msg.WithCalleeServiceName(cfg.Server.Service[0].Name)
} else {
msg.WithCalleeApp(cfg.Server.App)
msg.WithCalleeServer(cfg.Server.Server)
}
return ctx
}
// GetMetaData returns metadata from ctx by key.
func GetMetaData(ctx context.Context, key string) []byte {
msg := codec.Message(ctx)
if len(msg.ServerMetaData()) > 0 {
return msg.ServerMetaData()[key]
}
return nil
}
// SetMetaData sets metadata which will be returned to upstream.
// This method is not thread-safe.
// Notice: SetMetaData can only be called in the server side rpc entry goroutine,
// not in goroutine that calls the client.
func SetMetaData(ctx context.Context, key string, val []byte) {
msg := codec.Message(ctx)
if len(msg.ServerMetaData()) > 0 {
msg.ServerMetaData()[key] = val
return
}
md := make(map[string][]byte)
md[key] = val
msg.WithServerMetaData(md)
}
// Request returns RequestProtocol from ctx.
// If the RequestProtocol not found, a new RequestProtocol will be created and returned.
func Request(ctx context.Context) *trpcpb.RequestProtocol {
msg := codec.Message(ctx)
request, ok := msg.ServerReqHead().(*trpcpb.RequestProtocol)
if !ok {
return &trpcpb.RequestProtocol{}
}
return request
}
// Response returns ResponseProtocol from ctx.
// If the ResponseProtocol not found, a new ResponseProtocol will be created and returned.
func Response(ctx context.Context) *trpcpb.ResponseProtocol {
msg := codec.Message(ctx)
response, ok := msg.ServerRspHead().(*trpcpb.ResponseProtocol)
if !ok {
return &trpcpb.ResponseProtocol{}
}
return response
}
// CloneContext copies the context to get a context that retains the value and doesn't cancel.
// This is used when the handler is processed asynchronously to detach the original timeout control
// and retains the original context information.
//
// After the trpc handler function returns, ctx will be canceled, and put the ctx's Msg back into pool,
// and the associated Metrics and logger will be released.
//
// Before starting a goroutine to run the handler function asynchronously,
// this method must be called to copy context, detach the original timeout control,
// and retain the information in Msg for Metrics.
//
// Retain the logger context for printing the associated log,
// keep other value in context, such as tracing context, etc.
func CloneContext(ctx context.Context) context.Context {
oldMsg := codec.Message(ctx)
newCtx, newMsg := codec.WithNewMessage(detach(ctx))
codec.CopyMsg(newMsg, oldMsg)
return newCtx
}
type detachedContext struct{ parent context.Context }
func detach(ctx context.Context) context.Context { return detachedContext{ctx} }
// Deadline implements context.Deadline
func (v detachedContext) Deadline() (time.Time, bool) { return time.Time{}, false }
// Done implements context.Done
func (v detachedContext) Done() <-chan struct{} { return nil }
// Err implements context.Err
func (v detachedContext) Err() error { return nil }
// Value implements context.Value
func (v detachedContext) Value(key interface{}) interface{} { return v.parent.Value(key) }
// GoAndWait provides safe concurrent handling. Per input handler, it starts a goroutine.
// Then it waits until all handlers are done and will recover if any handler panics.
// The returned error is the first non-nil error returned by one of the handlers.
// It can be set that non-nil error will be returned if the "key" handler fails while other handlers always
// return nil error.
func GoAndWait(handlers ...func() error) error {
var (
wg sync.WaitGroup
once sync.Once
err error
)
for _, f := range handlers {
wg.Add(1)
go func(handler func() error) {
defer func() {
if e := recover(); e != nil {
buf := make([]byte, PanicBufLen)
buf = buf[:runtime.Stack(buf, false)]
log.Errorf("[PANIC]%v\n%s\n", e, buf)
report.PanicNum.Incr()
once.Do(func() {
err = errs.New(errs.RetServerSystemErr, "panic found in call handlers")
})
}
wg.Done()
}()
if e := handler(); e != nil {
once.Do(func() {
err = e
})
}
}(f)
}
wg.Wait()
return err
}
// Goer is the interface that launches a testable and safe goroutine.
type Goer interface {
Go(ctx context.Context, timeout time.Duration, handler func(context.Context)) error
}
type asyncGoer struct {
panicBufLen int
shouldRecover bool
pool *ants.PoolWithFunc
}
type goerParam struct {
ctx context.Context
cancel context.CancelFunc
handler func(context.Context)
}
// NewAsyncGoer creates a goer that executes handler asynchronously with a goroutine when Go() is called.
func NewAsyncGoer(workerPoolSize int, panicBufLen int, shouldRecover bool) Goer {
g := &asyncGoer{
panicBufLen: panicBufLen,
shouldRecover: shouldRecover,
}
if workerPoolSize == 0 {
return g
}
pool, err := ants.NewPoolWithFunc(workerPoolSize, func(args interface{}) {
p := args.(*goerParam)
g.handle(p.ctx, p.handler, p.cancel)
})
if err != nil {
panic(err)
}
g.pool = pool
return g
}
func (g *asyncGoer) handle(ctx context.Context, handler func(context.Context), cancel context.CancelFunc) {
defer func() {
if g.shouldRecover {
if err := recover(); err != nil {
buf := make([]byte, g.panicBufLen)
buf = buf[:runtime.Stack(buf, false)]
log.ErrorContextf(ctx, "[PANIC]%v\n%s\n", err, buf)
report.PanicNum.Incr()
}
}
cancel()
}()
handler(ctx)
}
func (g *asyncGoer) Go(ctx context.Context, timeout time.Duration, handler func(context.Context)) error {
oldMsg := codec.Message(ctx)
newCtx, newMsg := codec.WithNewMessage(detach(ctx))
codec.CopyMsg(newMsg, oldMsg)
newCtx, cancel := context.WithTimeout(newCtx, timeout)
if g.pool != nil {
p := &goerParam{
ctx: newCtx,
cancel: cancel,
handler: handler,
}
return g.pool.Invoke(p)
}
go g.handle(newCtx, handler, cancel)
return nil
}
type syncGoer struct {
}
// NewSyncGoer creates a goer that executes handler synchronously without cloning ctx when Go() is called.
// it's usually used for testing.
func NewSyncGoer() Goer {
return &syncGoer{}
}
func (g *syncGoer) Go(ctx context.Context, timeout time.Duration, handler func(context.Context)) error {
newCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
handler(newCtx)
return nil
}
// DefaultGoer is an async goer without worker pool.
var DefaultGoer = NewAsyncGoer(0, PanicBufLen, true)
// Go launches a safer goroutine for async task inside rpc handler.
// it clones ctx and msg before the goroutine, and will recover and report metrics when the goroutine panics.
// you should set a suitable timeout to control the lifetime of the new goroutine to prevent goroutine leaks.
func Go(ctx context.Context, timeout time.Duration, handler func(context.Context)) error {
return DefaultGoer.Go(ctx, timeout, handler)
}
// expandEnv looks for ${var} in s and replaces them with value of the
// corresponding environment variable.
// $var is considered invalid.
// It's not like os.ExpandEnv which will handle both ${var} and $var.
// Since configurations like password for redis/mysql may contain $, this
// method is needed.
func expandEnv(s string) string {
var buf []byte
i := 0
for j := 0; j < len(s); j++ {
if s[j] == '$' && j+2 < len(s) && s[j+1] == '{' { // only ${var} instead of $var is valid
if buf == nil {
buf = make([]byte, 0, 2*len(s))
}
buf = append(buf, s[i:j]...)
name, w := getEnvName(s[j+1:])
if name == "" && w > 0 {
// invalid matching, remove the $
} else if name == "" {
buf = append(buf, s[j]) // keep the $
} else {
buf = append(buf, os.Getenv(name)...)
}
j += w
i = j + 1
}
}
if buf == nil {
return s
}
return string(buf) + s[i:]
}
// getEnvName gets env name, that is, var from ${var}.
// And content of var and its len will be returned.
func getEnvName(s string) (string, int) {
// look for right curly bracket '}'
// it's guaranteed that the first char is '{' and the string has at least two char
for i := 1; i < len(s); i++ {
if s[i] == ' ' || s[i] == '\n' || s[i] == '"' { // "xx${xxx"
return "", 0 // encounter invalid char, keep the $
}
if s[i] == '}' {
if i == 1 { // ${}
return "", 2 // remove ${}
}
return s[1:i], i + 1
}
}
return "", 0 // no },keep the $
}
// --------------- the following code is IP Config related -----------------//
// nicIP defines the parameters used to record the ip address (ipv4 & ipv6) of the nic.
type nicIP struct {
nic string
ipv4 []string
ipv6 []string
}
// netInterfaceIP maintains the nic name to nicIP mapping.
type netInterfaceIP struct {
once sync.Once
ips map[string]*nicIP
}
// enumAllIP returns the nic name to nicIP mapping.
func (p *netInterfaceIP) enumAllIP() map[string]*nicIP {
p.once.Do(func() {
p.ips = make(map[string]*nicIP)
interfaces, err := net.Interfaces()
if err != nil {
return
}
for _, i := range interfaces {
p.addInterface(i)
}
})
return p.ips
}
func (p *netInterfaceIP) addInterface(i net.Interface) {
addrs, err := i.Addrs()
if err != nil {
return
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if ipNet.IP.To4() != nil {
p.addIPv4(i.Name, ipNet.IP.String())
} else if ipNet.IP.To16() != nil {
p.addIPv6(i.Name, ipNet.IP.String())
}
}
}
// addIPv4 append ipv4 address
func (p *netInterfaceIP) addIPv4(nic string, ip4 string) {
ips := p.getNicIP(nic)
ips.ipv4 = append(ips.ipv4, ip4)
}
// addIPv6 append ipv6 address
func (p *netInterfaceIP) addIPv6(nic string, ip6 string) {
ips := p.getNicIP(nic)
ips.ipv6 = append(ips.ipv6, ip6)
}
// getNicIP returns nicIP by nic name.
func (p *netInterfaceIP) getNicIP(nic string) *nicIP {
if _, ok := p.ips[nic]; !ok {
p.ips[nic] = &nicIP{nic: nic}
}
return p.ips[nic]
}
// getIPByNic returns ip address by nic name.
// If the ipv4 addr is not empty, it will be returned.
// Otherwise, the ipv6 addr will be returned.
func (p *netInterfaceIP) getIPByNic(nic string) string {
p.enumAllIP()
if len(p.ips) <= 0 {
return ""
}
if _, ok := p.ips[nic]; !ok {
return ""
}
ip := p.ips[nic]
if len(ip.ipv4) > 0 {
return ip.ipv4[0]
}
if len(ip.ipv6) > 0 {
return ip.ipv6[0]
}
return ""
}
// localIP records the local nic name->nicIP mapping.
var localIP = &netInterfaceIP{}
// getIP returns ip addr by nic name.
func getIP(nic string) string {
ip := localIP.getIPByNic(nic)
return ip
}
// deduplicate merges two slices.
// Order will be kept and duplication will be removed.
func deduplicate(a, b []string) []string {
r := make([]string, 0, len(a)+len(b))
m := make(map[string]bool)
for _, s := range append(a, b...) {
if _, ok := m[s]; !ok {
m[s] = true
r = append(r, s)
}
}
return r
}