-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathstream.go
189 lines (168 loc) · 5.27 KB
/
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
//
//
// 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 client
import (
"context"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/errs"
icodec "trpc.group/trpc-go/trpc-go/internal/codec"
"trpc.group/trpc-go/trpc-go/internal/report"
"trpc.group/trpc-go/trpc-go/transport"
)
// Stream is the interface that performs streaming RPCs.
type Stream interface {
// Send sends stream messages.
Send(ctx context.Context, m interface{}) error
// Recv receives stream messages.
Recv(ctx context.Context) ([]byte, error)
// Init initiates all stream related options.
Init(ctx context.Context, opt ...Option) (*Options, error)
// Invoke initiates the lower layer connection to build the stream.
Invoke(ctx context.Context) error
// Close closes the stream.
Close(ctx context.Context) error
}
// DefaultStream is the default client Stream.
var DefaultStream = NewStream()
// NewStream is the function that returns a Stream.
var NewStream = func() Stream {
return &stream{}
}
// stream is an implementation of Stream.
type stream struct {
opts *Options
client
}
// SendControl is the interface used for sender's flow control.
type SendControl interface {
GetWindow(uint32) error
UpdateWindow(uint32)
}
// RecvControl is the interface used for receiver's flow control.
type RecvControl interface {
OnRecv(n uint32) error
}
// Send implements Stream.
// It serializes the message and sends it to server through stream transport.
// It's safe to call Recv and Send in different goroutines concurrently, but calling
// Send in different goroutines concurrently is not thread-safe.
func (s *stream) Send(ctx context.Context, m interface{}) (err error) {
defer func() {
if err != nil {
s.opts.StreamTransport.Close(ctx)
}
}()
msg := codec.Message(ctx)
reqBodyBuf, err := serializeAndCompress(ctx, msg, m, s.opts)
if err != nil {
return err
}
// if m != nil, m is Data frame and sender flow control is needed.
if m != nil && s.opts.SControl != nil {
if err := s.opts.SControl.GetWindow(uint32(len(reqBodyBuf))); err != nil {
return err
}
}
// encode reqBodyBuf
reqBuf, err := s.opts.Codec.Encode(msg, reqBodyBuf)
if err != nil {
return errs.NewFrameError(errs.RetClientEncodeFail, "client codec Encode: "+err.Error())
}
if err := s.opts.StreamTransport.Send(ctx, reqBuf); err != nil {
return err
}
return nil
}
// Recv implements Stream.
// It decodes and decompresses the message and leaves serialization to upper layer.
// It's safe to call Recv and Send in different goroutines concurrently, but calling
// Send in different goroutines concurrently is not thread-safe.
func (s *stream) Recv(ctx context.Context) (buf []byte, err error) {
defer func() {
if err != nil {
s.opts.StreamTransport.Close(ctx)
}
}()
rspBuf, err := s.opts.StreamTransport.Recv(ctx)
if err != nil {
return nil, err
}
msg := codec.Message(ctx)
rspBodyBuf, err := s.opts.Codec.Decode(msg, rspBuf)
if err != nil {
return nil, errs.NewFrameError(errs.RetClientDecodeFail, "client codec Decode: "+err.Error())
}
if err := msg.ClientRspErr(); err != nil {
return nil, err
}
if len(rspBodyBuf) > 0 {
compressType := msg.CompressType()
if icodec.IsValidCompressType(s.opts.CurrentCompressType) {
compressType = s.opts.CurrentCompressType
}
// decompress
if icodec.IsValidCompressType(compressType) && compressType != codec.CompressTypeNoop {
rspBodyBuf, err = codec.Decompress(compressType, rspBodyBuf)
if err != nil {
return nil, errs.NewFrameError(errs.RetClientDecodeFail, "client codec Decompress: "+err.Error())
}
}
}
return rspBodyBuf, nil
}
// Close implements Stream.
func (s *stream) Close(ctx context.Context) error {
// Send Close message.
return s.Send(ctx, nil)
}
// Init implements Stream.
func (s *stream) Init(ctx context.Context, opt ...Option) (*Options, error) {
// The generic message structure data of the current request is retrieved from the context,
// and each backend call uses a new msg generated by the client stub code.
msg := codec.Message(ctx)
// Get options.
opts, err := s.getOptions(msg, opt...)
if err != nil {
return nil, err
}
// Update msg.
s.updateMsg(msg, opts)
// Select a node of backend service.
node, err := selectNode(ctx, msg, opts)
if err != nil {
report.SelectNodeFail.Incr()
return nil, err
}
ensureMsgRemoteAddr(msg, findFirstNonEmpty(node.Network, opts.Network), node.Address, node.ParseAddr)
const invalidCost = -1
opts.Node.set(node, node.Address, invalidCost)
if opts.Codec == nil {
report.ClientCodecEmpty.Incr()
return nil, errs.NewFrameError(errs.RetClientEncodeFail, "client: codec empty")
}
opts.CallOptions = append(opts.CallOptions, transport.WithMsg(msg))
s.opts = opts
return s.opts, nil
}
func findFirstNonEmpty(ss ...string) string {
for _, s := range ss {
if s != "" {
return s
}
}
return ""
}
// Invoke implements Stream.
func (s *stream) Invoke(ctx context.Context) error {
return s.opts.StreamTransport.Init(ctx, s.opts.CallOptions...)
}