-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_wrapper.go
316 lines (292 loc) · 8.01 KB
/
stream_wrapper.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
package totem
import (
"context"
"fmt"
"io"
sync "sync"
"sync/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
)
var ErrConnClosing = status.Error(codes.Unavailable, "transport is closing")
var ErrIllegalHeaderWrite = status.Error(codes.Internal, "SendHeader called multiple times")
type localServerStreamWrapper struct {
ctx context.Context
req *RPC
headers metadata.MD
trailers metadata.MD
wroteHeaders atomic.Bool
writer chan<- *RPC
closedWriter chan struct{}
}
func newLocalServerStreamWrapper(ctx context.Context, req *RPC, writer chan<- *RPC) *localServerStreamWrapper {
return &localServerStreamWrapper{
ctx: ctx,
req: req,
writer: writer,
closedWriter: make(chan struct{}),
}
}
func (s *localServerStreamWrapper) Close() (trailers metadata.MD) {
close(s.closedWriter)
return s.trailers
}
// Context implements grpc.ServerStream.
func (s *localServerStreamWrapper) Context() context.Context {
return s.ctx
}
// RecvMsg implements grpc.ServerStream.
func (s *localServerStreamWrapper) RecvMsg(m interface{}) error {
// unmarshal the request into m
switch m := m.(type) {
case *RPC:
m.Content = s.req.Content
case protoadapt.MessageV2:
if err := proto.Unmarshal(s.req.GetRequest(), m); err != nil {
return fmt.Errorf("[totem] malformed request: %w", err)
}
case protoadapt.MessageV1:
m2 := protoadapt.MessageV2Of(m)
if err := proto.Unmarshal(s.req.GetRequest(), m2); err != nil {
return fmt.Errorf("[totem] malformed request: %w", err)
}
default:
panic(fmt.Sprintf("[totem] unsupported request type: %T", m))
}
return nil
}
// SendHeader implements grpc.ServerStream.
func (s *localServerStreamWrapper) SendHeader(md metadata.MD) error {
if err := s.SetHeader(md); err != nil {
return err
}
if s.wroteHeaders.CompareAndSwap(false, true) {
select {
case s.writer <- &RPC{
Content: &RPC_ServerStreamMsg{},
Metadata: FromMD(s.headers),
}:
s.headers = nil
case <-s.closedWriter:
return ErrConnClosing
}
} else {
return ErrIllegalHeaderWrite
}
return nil
}
// SendMsg implements grpc.ServerStream.
func (s *localServerStreamWrapper) SendMsg(m interface{}) error {
bytes, err := proto.Marshal(protoimpl.X.ProtoMessageV2Of(m))
if err != nil {
return err
}
rpc := &RPC{
Content: &RPC_ServerStreamMsg{
ServerStreamMsg: &ServerStreamMessage{
Response: bytes,
},
},
}
if s.wroteHeaders.CompareAndSwap(false, true) {
rpc.Metadata = FromMD(s.headers)
s.headers = nil
}
select {
case s.writer <- rpc:
return nil
case <-s.closedWriter:
return ErrConnClosing
}
}
// SetHeader implements grpc.ServerStream.
func (s *localServerStreamWrapper) SetHeader(md metadata.MD) error {
if md.Len() == 0 {
return nil
}
if err := validateMetadata(md); err != nil {
return status.Error(codes.Internal, err.Error())
}
// set the header data, which will be sent when SendHeader is called or the
// first message is sent
s.headers = metadata.Join(s.headers, md)
return nil
}
// SetTrailer implements grpc.ServerStream.
func (s *localServerStreamWrapper) SetTrailer(md metadata.MD) {
if md.Len() == 0 {
return
}
// the actual implementation sets trailers regardless of whether they
// pass validation, so we do the same
s.trailers = metadata.Join(s.trailers, md)
}
var _ grpc.ServerStream = (*localServerStreamWrapper)(nil)
type serverStreamClientWrapper struct {
ctx context.Context
controller *StreamController
streamName string
methodName string
bufferedFirstMsg []byte
replyC <-chan *RPC
writeHeadersOnce sync.Once
headerC chan metadata.MD
recvHeadersOnce func() (metadata.MD, error)
trailer metadata.MD
onFinishedOnce sync.Once
onFinish []func(error)
}
func newServerStreamClientWrapper(
ctx context.Context,
ctrl *StreamController, streamName, methodName string,
opts ...grpc.CallOption,
) *serverStreamClientWrapper {
hc := make(chan metadata.MD, 1)
cs := &serverStreamClientWrapper{
ctx: ctx,
controller: ctrl,
streamName: streamName,
methodName: methodName,
headerC: hc,
recvHeadersOnce: sync.OnceValues(func() (metadata.MD, error) {
select {
case md := <-hc:
return md, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}),
}
for _, opt := range opts {
if o, ok := opt.(grpc.OnFinishCallOption); ok {
cs.onFinish = append(cs.onFinish, o.OnFinish)
}
}
return cs
}
// CloseSend implements grpc.ClientStream.
func (cs *serverStreamClientWrapper) CloseSend() error {
clientHeaders, ok := metadata.FromOutgoingContext(cs.ctx)
if !ok {
clientHeaders = metadata.New(nil)
}
cs.replyC = cs.controller.Request(cs.ctx, &RPC{
ServiceName: cs.streamName,
MethodName: cs.methodName,
Content: &RPC_Request{
Request: cs.bufferedFirstMsg,
},
Metadata: FromMD(clientHeaders),
})
return nil
}
// Context implements grpc.ClientStream.
func (cs *serverStreamClientWrapper) Context() context.Context {
return cs.ctx
}
// Header implements grpc.ClientStream.
func (cs *serverStreamClientWrapper) Header() (metadata.MD, error) {
select {
case md := <-cs.headerC:
return md, nil
case <-cs.ctx.Done():
return nil, cs.ctx.Err()
}
}
func (cs *serverStreamClientWrapper) finish(err error) {
cs.onFinishedOnce.Do(func() {
for _, fn := range cs.onFinish {
fn(err)
}
})
}
// RecvMsg implements grpc.ClientStream.
func (cs *serverStreamClientWrapper) RecvMsg(m interface{}) error {
select {
case rpc, ok := <-cs.replyC:
if !ok {
cs.finish(nil)
return io.EOF
}
switch rpc.Content.(type) {
case *RPC_ServerStreamMsg:
msg := rpc.GetServerStreamMsg()
switch m := m.(type) {
case *RPC:
m.Content = &RPC_ServerStreamMsg{
ServerStreamMsg: msg,
}
case protoadapt.MessageV2:
cs.writeHeadersOnce.Do(func() { cs.headerC <- msg.Headers.ToMD() })
if err := proto.Unmarshal(msg.GetResponse(), m); err != nil {
return fmt.Errorf("[totem] malformed response: %w", err)
}
case protoadapt.MessageV1:
cs.writeHeadersOnce.Do(func() { cs.headerC <- msg.Headers.ToMD() })
m2 := protoadapt.MessageV2Of(m)
if err := proto.Unmarshal(msg.GetResponse(), m2); err != nil {
return fmt.Errorf("[totem] malformed response: %w", err)
}
default:
panic(fmt.Sprintf("[totem] unsupported response type: %T", m))
}
case *RPC_Response:
resp := rpc.GetResponse()
status := resp.GetStatus()
if err := status.Err(); err != nil {
cs.finish(err)
return err
}
switch m := m.(type) {
case *RPC:
m.Content = &RPC_Response{
Response: resp,
}
case protoadapt.MessageV2:
if rpc.Metadata != nil {
cs.trailer = rpc.Metadata.ToMD()
}
if err := proto.Unmarshal(resp.GetResponse(), m); err != nil {
return fmt.Errorf("[totem] malformed response: %w", err)
}
case protoadapt.MessageV1:
if rpc.Metadata != nil {
cs.trailer = rpc.Metadata.ToMD()
}
m2 := protoadapt.MessageV2Of(m)
if err := proto.Unmarshal(resp.GetResponse(), m2); err != nil {
return fmt.Errorf("[totem] malformed response: %w", err)
}
default:
panic(fmt.Sprintf("[totem] unsupported response type: %T", m))
}
default:
return fmt.Errorf("[totem] unexpected response type: %T", rpc.Content)
}
case <-cs.ctx.Done():
return cs.ctx.Err()
}
return nil
}
// SendMsg implements grpc.ClientStream.
func (cs *serverStreamClientWrapper) SendMsg(m interface{}) error {
if cs.bufferedFirstMsg != nil {
return status.Errorf(codes.Internal, "unexpected call to SendMsg on server-streaming RPC")
}
var err error
cs.bufferedFirstMsg, err = proto.Marshal(protoimpl.X.ProtoMessageV2Of(m))
if err != nil {
return err
}
return nil
}
// Trailer implements grpc.ClientStream.
func (cs *serverStreamClientWrapper) Trailer() metadata.MD {
return cs.trailer
}
var _ grpc.ClientStream = (*serverStreamClientWrapper)(nil)