/
context.go
178 lines (147 loc) ยท 3.58 KB
/
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
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
package server
import (
"fmt"
"net"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/share"
)
// Context represents a rpcx FastCall context.
type Context struct {
conn net.Conn
req *protocol.Message
ctx *share.Context
async bool
}
// NewContext creates a server.Context for Handler.
func NewContext(ctx *share.Context, conn net.Conn, req *protocol.Message, async bool) *Context {
return &Context{conn: conn, req: req, ctx: ctx, async: async}
}
// Get returns value for key.
func (ctx *Context) Get(key interface{}) interface{} {
return ctx.ctx.Value(key)
}
// SetValue sets the kv pair.
func (ctx *Context) SetValue(key, val interface{}) {
if key == nil || val == nil {
return
}
ctx.ctx.SetValue(key, val)
}
// DeleteKey delete the kv pair by key.
func (ctx *Context) DeleteKey(key interface{}) {
if ctx.ctx == nil || key == nil {
return
}
ctx.ctx.DeleteKey(key)
}
// Payload returns the payload.
func (ctx *Context) Payload() []byte {
return ctx.req.Payload
}
// Metadata returns the metadata.
func (ctx *Context) Metadata() map[string]string {
return ctx.req.Metadata
}
// ServicePath returns the ServicePath.
func (ctx *Context) ServicePath() string {
return ctx.req.ServicePath
}
// ServiceMethod returns the ServiceMethod.
func (ctx *Context) ServiceMethod() string {
return ctx.req.ServiceMethod
}
// Bind parses the body data and stores the result to v.
func (ctx *Context) Bind(v interface{}) error {
req := ctx.req
if v != nil {
codec := share.Codecs[req.SerializeType()]
if codec == nil {
return fmt.Errorf("can not find codec for %d", req.SerializeType())
}
err := codec.Decode(req.Payload, v)
if err != nil {
return err
}
}
return nil
}
func (ctx *Context) Write(v interface{}) error {
req := ctx.req
if req.IsOneway() { // no need to send response
return nil
}
codec := share.Codecs[req.SerializeType()]
if codec == nil {
return fmt.Errorf("can not find codec for %d", req.SerializeType())
}
res := req.Clone()
res.SetMessageType(protocol.Response)
if v != nil {
data, err := codec.Encode(v)
if err != nil {
return err
}
res.Payload = data
}
resMetadata := ctx.Get(share.ResMetaDataKey)
if resMetadata != nil {
resMetaInCtx := resMetadata.(map[string]string)
meta := res.Metadata
if meta == nil {
res.Metadata = resMetaInCtx
} else {
for k, v := range resMetaInCtx {
if meta[k] == "" {
meta[k] = v
}
}
}
}
if len(res.Payload) > 1024 && req.CompressType() != protocol.None {
res.SetCompressType(req.CompressType())
}
respData := res.EncodeSlicePointer()
var err error
if ctx.async {
go func() {
_, err = ctx.conn.Write(*respData)
protocol.PutData(respData)
}()
} else {
_, err = ctx.conn.Write(*respData)
protocol.PutData(respData)
}
return err
}
func (ctx *Context) WriteError(err error) error {
req := ctx.req
if req.IsOneway() { // no need to send response
return nil
}
codec := share.Codecs[req.SerializeType()]
if codec == nil {
return fmt.Errorf("can not find codec for %d", req.SerializeType())
}
res := req.Clone()
res.SetMessageType(protocol.Response)
resMetadata := ctx.Get(share.ResMetaDataKey)
if resMetadata != nil {
resMetaInCtx := resMetadata.(map[string]string)
meta := res.Metadata
if meta == nil {
res.Metadata = resMetaInCtx
} else {
for k, v := range resMetaInCtx {
if meta[k] == "" {
meta[k] = v
}
}
}
}
res.SetMessageStatusType(protocol.Error)
res.Metadata[protocol.ServiceError] = err.Error()
respData := res.EncodeSlicePointer()
ctx.conn.Write(*respData)
protocol.PutData(respData)
return nil
}