Skip to content

Commit 781b7c7

Browse files
committed
update
1 parent e516fe1 commit 781b7c7

File tree

7 files changed

+51
-22
lines changed

7 files changed

+51
-22
lines changed

cgrpc/context.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func (s *CGrpcServer) getcontext(c context.Context, path string, peername string, peeraddr string, metadata map[string]string, handlers []OutsideHandler, d func(interface{}) error) *Context {
1111
ctx, ok := s.ctxpool.Get().(*Context)
1212
if !ok {
13-
return &Context{
13+
ctx = &Context{
1414
Context: c,
1515
decodefunc: d,
1616
handlers: handlers,
@@ -22,20 +22,29 @@ func (s *CGrpcServer) getcontext(c context.Context, path string, peername string
2222
e: nil,
2323
status: 0,
2424
}
25+
if metadata == nil {
26+
ctx.metadata = make(map[string]string)
27+
}
28+
return ctx
2529
}
2630
ctx.Context = c
2731
ctx.decodefunc = d
2832
ctx.handlers = handlers
2933
ctx.path = path
3034
ctx.peername = peername
3135
ctx.peeraddr = peeraddr
32-
ctx.metadata = metadata
36+
if metadata != nil {
37+
ctx.metadata = metadata
38+
}
3339
ctx.resp = nil
3440
ctx.e = nil
3541
ctx.status = 0
3642
return ctx
3743
}
3844
func (s *CGrpcServer) putcontext(ctx *Context) {
45+
for k := range ctx.metadata {
46+
delete(ctx.metadata, k)
47+
}
3948
s.ctxpool.Put(ctx)
4049
}
4150

cgrpc/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,11 @@ func (s *CGrpcServer) insidehandler(sname, mname string, handlers ...OutsideHand
239239
}
240240
}
241241
traceid, _, _, _, _, selfdeep = trace.GetTrace(ctx)
242-
mdata := make(map[string]string)
242+
var mdata map[string]string
243243
if ok {
244244
data := grpcmetadata.Get("core_metadata")
245245
if len(data) != 0 {
246+
mdata = make(map[string]string)
246247
if e := json.Unmarshal(common.Str2byte(data[0]), &mdata); e != nil {
247248
log.Error(nil, "[cgrpc.server] client:", sourceapp+":"+sourceip, "path:", path, "method: GRPC metadata:", data[0], "format error:", e)
248249
return nil, cerror.ErrReq

codegen/tml/gomod/template_gomod.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const text = `module {{.}}
1111
go 1.17
1212
1313
require (
14-
github.com/chenjie199234/Config v0.0.28
15-
github.com/chenjie199234/Corelib v0.0.42
14+
github.com/chenjie199234/Config v0.0.29
15+
github.com/chenjie199234/Corelib v0.0.43
1616
github.com/fsnotify/fsnotify v1.5.1
1717
github.com/go-sql-driver/mysql v1.6.0
1818
github.com/segmentio/kafka-go v0.4.25

crpc/context.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,44 @@ import (
88
"github.com/chenjie199234/Corelib/util/common"
99
)
1010

11-
func (s *CrpcServer) getContext(ctx context.Context, p *stream.Peer, msg *Msg, handlers []OutsideHandler) *Context {
12-
result, ok := s.ctxpool.Get().(*Context)
11+
func (s *CrpcServer) getContext(c context.Context, p *stream.Peer, msg *Msg, handlers []OutsideHandler) *Context {
12+
ctx, ok := s.ctxpool.Get().(*Context)
1313
if !ok {
14-
return &Context{
15-
Context: ctx,
14+
ctx = &Context{
15+
Context: c,
1616
peer: p,
1717
msg: msg,
18+
metadata: msg.Metadata,
1819
handlers: handlers,
1920
status: 0,
2021
}
22+
if msg.Metadata == nil {
23+
ctx.metadata = make(map[string]string)
24+
}
25+
return ctx
26+
}
27+
ctx.Context = c
28+
ctx.peer = p
29+
ctx.msg = msg
30+
if msg.Metadata != nil {
31+
ctx.metadata = msg.Metadata
2132
}
22-
result.Context = ctx
23-
result.peer = p
24-
result.msg = msg
25-
result.handlers = handlers
26-
result.status = 0
27-
return result
33+
ctx.handlers = handlers
34+
ctx.status = 0
35+
return ctx
2836
}
2937

3038
func (s *CrpcServer) putContext(ctx *Context) {
39+
for k := range ctx.metadata {
40+
delete(ctx.metadata, k)
41+
}
3142
s.ctxpool.Put(ctx)
3243
}
3344

3445
type Context struct {
3546
context.Context
3647
msg *Msg
48+
metadata map[string]string
3749
peer *stream.Peer
3850
handlers []OutsideHandler
3951
status int8
@@ -93,5 +105,5 @@ func (c *Context) GetPeerAddr() string {
93105
return c.peer.GetRemoteAddr()
94106
}
95107
func (c *Context) GetMetadata() map[string]string {
96-
return c.msg.Metadata
108+
return c.metadata
97109
}

crpc/server.go

-3
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,6 @@ func (s *CrpcServer) userfunc(p *stream.Peer, data []byte) {
414414
c.calls[msg.Callid] = cancel
415415
c.Unlock()
416416
go func() {
417-
if msg.Metadata == nil {
418-
msg.Metadata = make(map[string]string)
419-
}
420417
handler(ctx, p, msg)
421418
d, _ := proto.Marshal(msg)
422419
if e := p.SendMessage(ctx, d, nil, nil); e != nil {

web/context.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func (s *WebServer) getContext(w http.ResponseWriter, r *http.Request, c context.Context, peername string, metadata map[string]string, handlers []OutsideHandler) *Context {
1616
ctx, ok := s.ctxpool.Get().(*Context)
1717
if !ok {
18-
return &Context{
18+
ctx = &Context{
1919
Context: c,
2020
w: w,
2121
r: r,
@@ -25,19 +25,28 @@ func (s *WebServer) getContext(w http.ResponseWriter, r *http.Request, c context
2525
status: 0,
2626
e: nil,
2727
}
28+
if metadata == nil {
29+
ctx.metadata = make(map[string]string)
30+
}
31+
return ctx
2832
}
2933
ctx.Context = c
3034
ctx.w = w
3135
ctx.r = r
3236
ctx.peername = peername
33-
ctx.metadata = metadata
37+
if metadata != nil {
38+
ctx.metadata = metadata
39+
}
3440
ctx.handlers = handlers
3541
ctx.status = 0
3642
ctx.e = nil
3743
return ctx
3844
}
3945

4046
func (s *WebServer) putContext(ctx *Context) {
47+
for k := range ctx.metadata {
48+
delete(ctx.metadata, k)
49+
}
4150
s.ctxpool.Put(ctx)
4251
}
4352

web/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,9 @@ func (s *WebServer) insideHandler(method, path string, handlers []OutsideHandler
513513
sourcepath = tracedata[3]
514514
}
515515
traceid, _, _, _, _, selfdeep = trace.GetTrace(ctx)
516-
mdata := make(map[string]string)
516+
var mdata map[string]string
517517
if mdstr := r.Header.Get("Core_metadata"); mdstr != "" {
518+
mdata = make(map[string]string)
518519
if e := json.Unmarshal(common.Str2byte(mdstr), &mdata); e != nil {
519520
log.Error(ctx, "[web.server] client ip:", getclientip(r), "path:", path, "method:", method, "error: metadata:", mdstr, "format error")
520521
w.WriteHeader(http.StatusBadRequest)

0 commit comments

Comments
 (0)