/
context.go
59 lines (53 loc) · 998 Bytes
/
context.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
package protocol
import (
"context"
"sync"
"github.com/panjf2000/ants/v2"
"github.com/panjf2000/gnet/v2"
"github.com/valyala/bytebufferpool"
)
var (
CtxPool = sync.Pool{
New: func() any {
return &Context{
H: &Header{},
Metadata: map[string]string{},
Payload: bytebufferpool.Get(),
}
},
}
)
func GetCtx() *Context {
ctx := CtxPool.Get().(*Context)
return ctx
}
func PutCtx(ctx *Context) {
ctx.Reset()
CtxPool.Put(ctx)
}
type Context struct {
H *Header
Payload *bytebufferpool.ByteBuffer
Conn gnet.Conn
ServicePath string
ServiceMethod string
Metadata map[string]string
//SerializeType uint8
MsgSeq uint64 //reserved field
Ctx context.Context
GPool *ants.Pool
}
func (c *Context) Reset() {
c.H.SerializeType = 0
c.H.MagicNumber = 0
c.H.HeartBeat = 0
c.H.Version = 0
c.Payload = nil
c.Conn = nil
c.ServicePath = ""
c.ServiceMethod = ""
c.Metadata = nil
c.MsgSeq = 0
c.Ctx = nil
c.GPool = nil
}