-
Notifications
You must be signed in to change notification settings - Fork 93
/
codec_stream.go
496 lines (453 loc) · 16.6 KB
/
codec_stream.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//
//
// 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,
// A copy of the Apache 2.0 License is included in this file.
//
//
package trpc
import (
"errors"
"fmt"
"os"
"path"
"sync"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/errs"
"trpc.group/trpc-go/trpc-go/internal/addrutil"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"
"google.golang.org/protobuf/proto"
)
var (
// error for unknown stream frame type
errUnknownFrameType error = errors.New("unknown stream frame type")
// error for invalid total length of client decoding
errClientDecodeTotalLength error = errors.New("client decode total length invalid")
// error for failing to encode Close frame
errEncodeCloseFrame error = errors.New("encode close frame error")
// error for failing to encode Feedback frame
errEncodeFeedbackFrame error = errors.New("encode feedback error")
// error for init metadata not found
errUninitializedMeta error = errors.New("uninitialized meta")
// error for invalid trpc framehead
errFrameHeadTypeInvalid error = errors.New("framehead type invalid")
)
// NewServerStreamCodec initializes and returns a ServerStreamCodec.
func NewServerStreamCodec() *ServerStreamCodec {
return &ServerStreamCodec{initMetas: make(map[string]map[uint32]*trpcpb.TrpcStreamInitMeta), m: &sync.RWMutex{}}
}
// NewClientStreamCodec initializes and returns a ClientStreamCodec.
func NewClientStreamCodec() *ClientStreamCodec {
return &ClientStreamCodec{}
}
// ServerStreamCodec is an implementation of codec.Codec.
// Used for trpc server streaming codec.
type ServerStreamCodec struct {
m *sync.RWMutex
initMetas map[string]map[uint32]*trpcpb.TrpcStreamInitMeta // addr->streamID->TrpcStreamInitMeta
}
// ClientStreamCodec is an implementation of codec.Codec.
// Used for trpc client streaming codec.
type ClientStreamCodec struct {
}
// Encode implements codec.Codec.
func (c *ClientStreamCodec) Encode(msg codec.Msg, reqBuf []byte) ([]byte, error) {
frameHead, ok := msg.FrameHead().(*FrameHead)
if !ok || !frameHead.isStream() {
return nil, errUnknownFrameType
}
switch trpcpb.TrpcStreamFrameType(frameHead.StreamFrameType) {
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT:
return c.encodeInitFrame(frameHead, msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA:
return c.encodeDataFrame(frameHead, msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE:
return c.encodeCloseFrame(frameHead, msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK:
return c.encodeFeedbackFrame(frameHead, msg, reqBuf)
default:
return nil, errUnknownFrameType
}
}
// Decode implements codec.Codec.
func (c *ClientStreamCodec) Decode(msg codec.Msg, rspBuf []byte) ([]byte, error) {
frameHead, ok := msg.FrameHead().(*FrameHead)
if !ok || !frameHead.isStream() {
return nil, errUnknownFrameType
}
msg.WithStreamID(frameHead.StreamID)
switch trpcpb.TrpcStreamFrameType(frameHead.StreamFrameType) {
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT:
return c.decodeInitFrame(msg, rspBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA:
return c.decodeDataFrame(msg, rspBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE:
return c.decodeCloseFrame(msg, rspBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK:
return c.decodeFeedbackFrame(msg, rspBuf)
default:
return nil, errUnknownFrameType
}
}
// decodeCloseFrame decodes the Close frame.
func (c *ClientStreamCodec) decodeCloseFrame(msg codec.Msg, rspBuf []byte) ([]byte, error) {
// unmarshal Close frame
close := &trpcpb.TrpcStreamCloseMeta{}
if err := proto.Unmarshal(rspBuf[frameHeadLen:], close); err != nil {
return nil, err
}
// It is considered an exception and an error should be returned to the client if:
// 1. the CloseType is Reset
// 2. ret code != 0
if close.GetCloseType() == int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_RESET) || close.GetRet() != 0 {
e := &errs.Error{
Type: errs.ErrorTypeCalleeFramework,
Code: trpcpb.TrpcRetCode(close.GetRet()),
Desc: "trpc",
Msg: string(close.GetMsg()),
}
msg.WithClientRspErr(e)
}
msg.WithStreamFrame(close)
return nil, nil
}
// decodeFeedbackFrame decodes the Feedback frame.
func (c *ClientStreamCodec) decodeFeedbackFrame(msg codec.Msg, rspBuf []byte) ([]byte, error) {
feedback := &trpcpb.TrpcStreamFeedBackMeta{}
if err := proto.Unmarshal(rspBuf[frameHeadLen:], feedback); err != nil {
return nil, err
}
msg.WithStreamFrame(feedback)
return nil, nil
}
// decodeInitFrame decodes the Init frame.
func (c *ClientStreamCodec) decodeInitFrame(msg codec.Msg, rspBuf []byte) ([]byte, error) {
initMeta := &trpcpb.TrpcStreamInitMeta{}
if err := proto.Unmarshal(rspBuf[frameHeadLen:], initMeta); err != nil {
return nil, err
}
msg.WithCompressType(int(initMeta.GetContentEncoding()))
msg.WithSerializationType(int(initMeta.GetContentType()))
// if ret code is not 0, an error should be set and returned
if initMeta.GetResponseMeta().GetRet() != 0 {
e := &errs.Error{
Type: errs.ErrorTypeCalleeFramework,
Code: trpcpb.TrpcRetCode(initMeta.GetResponseMeta().GetRet()),
Desc: "trpc",
Msg: string(initMeta.GetResponseMeta().GetErrorMsg()),
}
msg.WithClientRspErr(e)
}
msg.WithStreamFrame(initMeta)
return nil, nil
}
// decodeDataFrame decodes the Data frame.
func (c *ClientStreamCodec) decodeDataFrame(msg codec.Msg, rspBuf []byte) ([]byte, error) {
// decoding Data frame is straightforward,
// as it just returns all data following the frame head
return rspBuf[frameHeadLen:], nil
}
// encodeInitFrame encodes the Init frame.
func (c *ClientStreamCodec) encodeInitFrame(frameHead *FrameHead, msg codec.Msg, reqBuf []byte) ([]byte, error) {
initMeta, ok := msg.StreamFrame().(*trpcpb.TrpcStreamInitMeta)
if !ok {
initMeta = &trpcpb.TrpcStreamInitMeta{}
initMeta.RequestMeta = &trpcpb.TrpcStreamInitRequestMeta{}
}
req := initMeta.RequestMeta
// set caller service name
// if nil, use the name of the process
if msg.CallerServiceName() == "" {
msg.WithCallerServiceName(fmt.Sprintf("trpc.app.%s.service", path.Base(os.Args[0])))
}
req.Caller = []byte(msg.CallerServiceName())
// set callee service name
req.Callee = []byte(msg.CalleeServiceName())
// set backend rpc name, ClientRPCName already set by upper layer of client stub
req.Func = []byte(msg.ClientRPCName())
// set backend serialization type
initMeta.ContentType = uint32(msg.SerializationType())
// set backend compression type
initMeta.ContentEncoding = uint32(msg.CompressType())
// set dyeing info
if msg.Dyeing() {
req.MessageType = req.MessageType | uint32(trpcpb.TrpcMessageType_TRPC_DYEING_MESSAGE)
}
// set client transinfo
req.TransInfo = setClientTransInfo(msg, req.TransInfo)
streamBuf, err := proto.Marshal(initMeta)
if err != nil {
return nil, err
}
return frameWrite(frameHead, streamBuf)
}
// encodeDataFrame encodes the Data frame.
func (c *ClientStreamCodec) encodeDataFrame(frameHead *FrameHead, msg codec.Msg, reqBuf []byte) ([]byte, error) {
return frameWrite(frameHead, reqBuf)
}
// encodeCloseFrame encodes the Close frame.
func (c *ClientStreamCodec) encodeCloseFrame(frameHead *FrameHead, msg codec.Msg,
reqBuf []byte) (rspbuf []byte, err error) {
closeFrame, ok := msg.StreamFrame().(*trpcpb.TrpcStreamCloseMeta)
if !ok {
return nil, errEncodeCloseFrame
}
streamBuf, err := proto.Marshal(closeFrame)
if err != nil {
return nil, err
}
return frameWrite(frameHead, streamBuf)
}
// encodeFeedbackFrame encodes the Feedback frame.
func (c *ClientStreamCodec) encodeFeedbackFrame(frameHead *FrameHead, msg codec.Msg, reqBuf []byte) ([]byte, error) {
feedbackFrame, ok := msg.StreamFrame().(*trpcpb.TrpcStreamFeedBackMeta)
if !ok {
return nil, errEncodeFeedbackFrame
}
streamBuf, err := proto.Marshal(feedbackFrame)
if err != nil {
return nil, err
}
return frameWrite(frameHead, streamBuf)
}
// frameWrite converts FrameHead to binary frame.
func frameWrite(frameHead *FrameHead, streamBuf []byte) ([]byte, error) {
// no pb header for streaming rpc
return frameHead.construct(nil, streamBuf, nil)
}
// encodeCloseFrame encodes the Close frame.
func (s *ServerStreamCodec) encodeCloseFrame(frameHead *FrameHead, msg codec.Msg, reqBuf []byte) ([]byte, error) {
defer s.deleteInitMeta(msg)
closeFrame, ok := msg.StreamFrame().(*trpcpb.TrpcStreamCloseMeta)
if !ok {
return nil, errEncodeCloseFrame
}
msg.WithStreamID(frameHead.StreamID)
streamBuf, err := proto.Marshal(closeFrame)
if err != nil {
return nil, err
}
return frameWrite(frameHead, streamBuf)
}
// encodeDataFrame encodes the Data frame.
func (s *ServerStreamCodec) encodeDataFrame(frameHead *FrameHead, msg codec.Msg, reqBuf []byte) ([]byte, error) {
// If there is an error when processing the Data frame,
// then return the Close frame and close the current stream.
if err := msg.ServerRspErr(); err != nil {
s.buildResetFrame(msg, frameHead, err)
return s.encodeCloseFrame(frameHead, msg, reqBuf)
}
return frameWrite(frameHead, reqBuf)
}
// encodeInitFrame encodes the Init frame.
func (s *ServerStreamCodec) encodeInitFrame(frameHead *FrameHead, msg codec.Msg, reqBuf []byte) ([]byte, error) {
rsp := getStreamInitMeta(msg)
rsp.ContentType = uint32(msg.SerializationType())
rsp.ContentEncoding = uint32(msg.CompressType())
rspMeta := &trpcpb.TrpcStreamInitResponseMeta{}
if e := msg.ServerRspErr(); e != nil {
rspMeta.Ret = int32(e.Code)
rspMeta.ErrorMsg = []byte(e.Msg)
}
rsp.ResponseMeta = rspMeta
streamBuf, err := proto.Marshal(rsp)
if err != nil {
return nil, err
}
return frameWrite(frameHead, streamBuf)
}
// encodeFeedbackFrame encodes the Feedback frame.
func (s *ServerStreamCodec) encodeFeedbackFrame(frameHead *FrameHead, msg codec.Msg, reqBuf []byte) ([]byte, error) {
feedback, ok := msg.StreamFrame().(*trpcpb.TrpcStreamFeedBackMeta)
if !ok {
return nil, errEncodeFeedbackFrame
}
streamBuf, err := proto.Marshal(feedback)
if err != nil {
return nil, err
}
return frameWrite(frameHead, streamBuf)
}
// getStreamInitMeta returns TrpcStreamInitMeta from msg.
// If not found, a new TrpcStreamInitMeta will be created and returned.
func getStreamInitMeta(msg codec.Msg) *trpcpb.TrpcStreamInitMeta {
rsp, ok := msg.StreamFrame().(*trpcpb.TrpcStreamInitMeta)
if !ok {
rsp = &trpcpb.TrpcStreamInitMeta{ResponseMeta: &trpcpb.TrpcStreamInitResponseMeta{}}
}
return rsp
}
// Encode implements codec.Codec.
func (s *ServerStreamCodec) Encode(msg codec.Msg, reqBuf []byte) (rspbuf []byte, err error) {
frameHead, ok := msg.FrameHead().(*FrameHead)
if !ok || !frameHead.isStream() {
return nil, errUnknownFrameType
}
switch trpcpb.TrpcStreamFrameType(frameHead.StreamFrameType) {
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT:
return s.encodeInitFrame(frameHead, msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA:
return s.encodeDataFrame(frameHead, msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE:
return s.encodeCloseFrame(frameHead, msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK:
return s.encodeFeedbackFrame(frameHead, msg, reqBuf)
default:
return nil, errUnknownFrameType
}
}
// Decode implements codec.Codec.
// It decodes the head and the stream frame data.
func (s *ServerStreamCodec) Decode(msg codec.Msg, reqBuf []byte) ([]byte, error) {
frameHead, ok := msg.FrameHead().(*FrameHead)
if !ok || !frameHead.isStream() {
return nil, errUnknownFrameType
}
msg.WithStreamID(frameHead.StreamID)
switch trpcpb.TrpcStreamFrameType(frameHead.StreamFrameType) {
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT:
return s.decodeInitFrame(msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA:
return s.decodeDataFrame(msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE:
return s.decodeCloseFrame(msg, reqBuf)
case trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK:
return s.decodeFeedbackFrame(msg, reqBuf)
default:
return nil, errUnknownFrameType
}
}
// decodeFeedbackFrame decodes the Feedback frame.
func (s *ServerStreamCodec) decodeFeedbackFrame(msg codec.Msg, reqBuf []byte) ([]byte, error) {
if err := s.setInitMeta(msg); err != nil {
return nil, err
}
feedback := &trpcpb.TrpcStreamFeedBackMeta{}
if err := proto.Unmarshal(reqBuf[frameHeadLen:], feedback); err != nil {
return nil, err
}
msg.WithStreamFrame(feedback)
return nil, nil
}
// setInitMeta finds the InitMeta and sets the ServerRPCName by the server handler in the InitMeta.
func (s *ServerStreamCodec) setInitMeta(msg codec.Msg) error {
streamID := msg.StreamID()
addr := addrutil.AddrToKey(msg.LocalAddr(), msg.RemoteAddr())
s.m.RLock()
defer s.m.RUnlock()
if streamIDToInitMeta, ok := s.initMetas[addr]; ok {
if initMeta, ok := streamIDToInitMeta[streamID]; ok {
msg.WithServerRPCName(string(initMeta.GetRequestMeta().GetFunc()))
return nil
}
}
return errUninitializedMeta
}
// deleteInitMeta deletes the cached info by msg.
func (s *ServerStreamCodec) deleteInitMeta(msg codec.Msg) {
addr := addrutil.AddrToKey(msg.LocalAddr(), msg.RemoteAddr())
streamID := msg.StreamID()
s.m.Lock()
defer s.m.Unlock()
delete(s.initMetas[addr], streamID)
if len(s.initMetas[addr]) == 0 {
delete(s.initMetas, addr)
}
}
// decodeCloseFrame decodes the Close frame.
func (s *ServerStreamCodec) decodeCloseFrame(msg codec.Msg, rspBuf []byte) ([]byte, error) {
if err := s.setInitMeta(msg); err != nil {
return nil, err
}
close := &trpcpb.TrpcStreamCloseMeta{}
if err := proto.Unmarshal(rspBuf[frameHeadLen:], close); err != nil {
return nil, err
}
// It is considered an exception and an error should be returned to the client if:
// 1. the CloseType is Reset
// 2. ret code != 0
if close.GetCloseType() == int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_RESET) || close.GetRet() != 0 {
e := &errs.Error{
Type: errs.ErrorTypeCalleeFramework,
Code: trpcpb.TrpcRetCode(close.GetRet()),
Desc: "trpc",
Msg: string(close.GetMsg()),
}
msg.WithServerRspErr(e)
}
msg.WithStreamFrame(close)
return nil, nil
}
// decodeDataFrame decodes the Data frame.
func (s *ServerStreamCodec) decodeDataFrame(msg codec.Msg, reqBuf []byte) ([]byte, error) {
if err := s.setInitMeta(msg); err != nil {
return nil, err
}
reqBody := reqBuf[frameHeadLen:]
return reqBody, nil
}
// decodeInitFrame decodes the Init frame.
func (s *ServerStreamCodec) decodeInitFrame(msg codec.Msg, reqBuf []byte) ([]byte, error) {
initMeta := &trpcpb.TrpcStreamInitMeta{}
if err := proto.Unmarshal(reqBuf[frameHeadLen:], initMeta); err != nil {
return nil, err
}
s.updateMsg(msg, initMeta)
s.storeInitMeta(msg, initMeta)
msg.WithStreamFrame(initMeta)
return nil, nil
}
// storeInitMeta stores the InitMeta every time when a new frame is received.
func (s *ServerStreamCodec) storeInitMeta(msg codec.Msg, initMeta *trpcpb.TrpcStreamInitMeta) {
streamID := msg.StreamID()
addr := addrutil.AddrToKey(msg.LocalAddr(), msg.RemoteAddr())
s.m.Lock()
defer s.m.Unlock()
if _, ok := s.initMetas[addr]; ok {
s.initMetas[addr][streamID] = initMeta
} else {
t := make(map[uint32]*trpcpb.TrpcStreamInitMeta)
t[streamID] = initMeta
s.initMetas[addr] = t
}
}
// updateMsg updates the Msg by InitMeta.
func (s *ServerStreamCodec) updateMsg(msg codec.Msg, initMeta *trpcpb.TrpcStreamInitMeta) {
// get request meta
req := initMeta.GetRequestMeta()
// set caller service name
msg.WithCallerServiceName(string(req.GetCaller()))
msg.WithCalleeServiceName(string(req.GetCallee()))
// set server handler method name
msg.WithServerRPCName(string(req.GetFunc()))
// set body serialization type
msg.WithSerializationType(int(initMeta.GetContentType()))
// set body compression type
msg.WithCompressType(int(initMeta.GetContentEncoding()))
msg.WithDyeing((req.GetMessageType() & uint32(trpcpb.TrpcMessageType_TRPC_DYEING_MESSAGE)) != 0)
if len(req.TransInfo) > 0 {
msg.WithServerMetaData(req.GetTransInfo())
// set dyeing key
if bs, ok := req.TransInfo[DyeingKey]; ok {
msg.WithDyeingKey(string(bs))
}
// set environment message for transfer
if envs, ok := req.TransInfo[EnvTransfer]; ok {
msg.WithEnvTransfer(string(envs))
}
}
}
func (s *ServerStreamCodec) buildResetFrame(msg codec.Msg, frameHead *FrameHead, err error) {
frameHead.StreamFrameType = uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE)
closeMeta := &trpcpb.TrpcStreamCloseMeta{
CloseType: int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_RESET),
Ret: int32(errs.Code(err)),
Msg: []byte(errs.Msg(err)),
}
msg.WithStreamFrame(closeMeta)
}