-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmoa_codec.go
More file actions
275 lines (242 loc) · 6.53 KB
/
Copy pathmoa_codec.go
File metadata and controls
275 lines (242 loc) · 6.53 KB
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
package core
import (
"context"
"encoding/json"
"github.com/blackbeans/turbo"
"github.com/golang/snappy"
"github.com/opentracing/opentracing-go"
"time"
)
const (
REQ = byte(0x01)
RESP = byte(0x02)
PING = byte(0x03)
PONG = byte(0x04)
INFO = byte(0x05)
)
const (
COMPRESS_SNAPPY = 0x01 //snappy算法
)
type BinaryCodec struct {
MaxFrameLength int
SnappyCompress bool
}
//snappy解压缩
func Decompress(src []byte) ([]byte, error) {
l, err := snappy.DecodedLen(src)
if nil != err {
return nil, err
}
if l%256 != 0 {
l = (l/256 + 1) * 256
}
dest := make([]byte, l)
decompressData, err := snappy.Decode(dest, src)
return decompressData, err
}
//snapp压缩
func Compress(src []byte) []byte {
l := snappy.MaxEncodedLen(len(src))
if l%256 != 0 {
l = (l/256 + 1) * 256
}
dest := make([]byte, l)
compressData := snappy.Encode(dest, src)
return compressData
}
//反序列化
//包装为packet,但是头部没有信息
func (self BinaryCodec) UnmarshalPayload(p *turbo.Packet) (interface{}, error) {
useSnappy := p.Header.Extension & COMPRESS_SNAPPY
//使用snap
if useSnappy == COMPRESS_SNAPPY {
d, err := Decompress(p.Data)
if nil != err {
return nil, err
}
p.Data = d
}
if p.Header.CmdType == REQ {
//req
req, err := Wrap2MoaRawRequest(p.Data)
if nil != err {
return turbo.Packet{}, err
}
if req.CreateTime <= 0 {
req.CreateTime = time.Now().UnixNano() / int64(time.Millisecond)
}
p.PayLoad = *req
} else if p.Header.CmdType == PING || p.Header.CmdType == PONG {
//ping
var ping PiPo
json.Unmarshal(p.Data, &ping)
p.PayLoad = ping
} else if p.Header.CmdType == RESP {
//resp
resp, err := Wrap2MoaRawResponse(p.Data)
if nil != err {
return p, err
}
p.PayLoad = *resp
}
return p.PayLoad, nil
}
func (self BinaryCodec) MarshalPayload(p *turbo.Packet) ([]byte, error) {
var rawPayload []byte
if p.Header.CmdType == REQ {
data, err := json.Marshal(p.PayLoad)
if nil != err {
return nil, err
}
rawPayload = data
} else if p.Header.CmdType == PING || p.Header.CmdType == PONG {
//pong协议
rawPayload, _ = json.Marshal(p.PayLoad)
} else if p.Header.CmdType == RESP {
resp, ok := p.PayLoad.(MoaRespPacket)
if !ok {
resp = MoaRespPacket{ErrCode: CODE_SERIALIZATION_SERVER,
Message: "Invalid PayLoad Type Not MoaRespPacket"}
}
data, err := json.Marshal(resp)
if nil != err {
log.Errorf("BinaryCodec|MarshalPacket|Marshal|FAIL|%v", err)
resp = MoaRespPacket{ErrCode: CODE_SERIALIZATION_SERVER,
Message: "Invalid PayLoad Type Not MoaRespPacket"}
data, _ = json.Marshal(resp)
}
rawPayload = data
}
return rawPayload, nil
}
type PiPo struct {
Timestamp int64 `json:"timestamp"`
}
type MoaReqPacket struct {
ServiceUri string `json:"action"`
Params struct {
Method string `json:"m"`
Args []interface{} `json:"args"`
} `json:"params"`
Properties map[string]string `json:"props,omitempty"`
CreateTime int64 `json:"-"` //创建时间 ms
Timeout time.Duration `json:"-"`
}
//moa请求协议的包
type MoaRawReqPacket struct {
ServiceUri string `json:"action"`
Params struct {
Method string `json:"m"`
Args []json.RawMessage `json:"args"`
} `json:"params"`
Properties map[string]string `json:"props,omitempty"`
CreateTime int64 `json:"-"` //创建时间 ms
Timeout time.Duration `json:"-"`
Source string `json:"-"`
}
//moa响应packet
type MoaRespPacket struct {
ErrCode int `json:"ec"`
Message string `json:"em"`
CreateTime int64 `json:"-"` //创建时间 ms
Result interface{} `json:"result"`
}
//moa响应packet
type MoaRawRespPacket struct {
ErrCode int `json:"ec"`
Message string `json:"em"`
CreateTime int64 `json:"-"` //创建时间 ms
Result json.RawMessage `json:"result"`
}
func Wrap2MoaRawRequest(data []byte) (*MoaRawReqPacket, error) {
var req MoaRawReqPacket
err := json.Unmarshal(data, &req)
if nil != err {
return nil, err
} else {
return &req, nil
}
}
func Wrap2MoaRawResponse(data []byte) (*MoaRawRespPacket, error) {
var resp MoaRawRespPacket
err := json.Unmarshal(data, &resp)
if nil != err {
return nil, err
}
return &resp, nil
}
const (
KEY_MOA_PROPERTIES = "moa.props"
//MOA节点选择hash值
KEY_MOA_PROPERTY_HASHID = "hashid"
//MOA的调用环境,可以一直带到整个调用链结束
KEY_MOA_PROPERTY_ENV_PRE = "moa.env.pre"
)
//切记切记。在使用完之后要做移除。否则会造成内存泄露
//调用 DetachGoProperties
// 注:如果要修改 moa context 中信息的存储方式,需要同时修改下面的 GetSpanCtx 和 WithSpanCtx
func AttachMoaProperty(ctx context.Context, key, val string) context.Context {
props := ctx.Value(KEY_MOA_PROPERTIES)
if nil != props {
if v, ok := props.(map[string]string); ok {
v[key] = val
return ctx
}
}
prop := make(map[string]string)
prop[key] = val
return context.WithValue(ctx, KEY_MOA_PROPERTIES, prop)
}
//剔除属性
func DetachMoaProperty(ctx context.Context, key string) {
props := ctx.Value(KEY_MOA_PROPERTIES)
if nil != props {
if v, ok := props.(map[string]string); ok {
delete(v, key)
}
}
}
//获取moa的上下文属性
func GetMoaProperty(ctx context.Context, key string) (string, bool) {
props := ctx.Value(KEY_MOA_PROPERTIES)
if nil != props {
if v, ok := props.(map[string]string); ok {
val, exist := v[key]
return val, exist
}
}
return "", false
}
// 从我们的 Context 中获取 SpanContext,如果没有则返回 nil
// 其实是从 context 的 moa.props 中获取信息
func GetSpanCtx(ctx context.Context) opentracing.SpanContext {
props := ctx.Value(KEY_MOA_PROPERTIES)
if props != nil {
if v, ok := props.(map[string]string); ok {
spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.TextMap, opentracing.TextMapCarrier(v))
if err != nil {
return nil
}
return spanCtx
}
}
return nil
}
// 将 SpanContext 存到我们的 Context 中,进行传递
// 其实是将一个键值对设置到了 context 的 moa.props 中
func WithSpanCtx(ctx context.Context, spCtx opentracing.SpanContext) context.Context {
props := ctx.Value(KEY_MOA_PROPERTIES)
var p map[string]string
if props != nil {
if v, ok := props.(map[string]string); ok {
p = v
} else {
// KEY_MOA_PROPERTIES 中不是 map[string]string
p = make(map[string]string)
}
} else {
p = make(map[string]string)
}
opentracing.GlobalTracer().Inject(spCtx, opentracing.TextMap, opentracing.TextMapCarrier(p))
return context.WithValue(ctx, KEY_MOA_PROPERTIES, p)
}