-
Notifications
You must be signed in to change notification settings - Fork 40
/
wrtc_client_stream.go
362 lines (329 loc) · 9.79 KB
/
wrtc_client_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
package rpc
import (
"context"
"errors"
"io"
"math"
"github.com/edaniels/golog"
protov1 "github.com/golang/protobuf/proto" //nolint:staticcheck
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"go.viam.com/utils"
webrtcpb "go.viam.com/utils/proto/rpc/webrtc/v1"
)
var _ = grpc.ClientStream(&webrtcClientStream{})
// A webrtcClientStream is the high level gRPC streaming interface used for both
// unary and streaming call requests.
type webrtcClientStream struct {
*webrtcBaseStream
ctx context.Context
cancel func()
ch *webrtcClientChannel
headers metadata.MD
trailers metadata.MD
userCtx context.Context
headersReceived chan struct{}
trailersReceived bool
sendClosed bool
}
// newWebRTCClientStream creates a gRPC stream from the given client channel with a
// unique identity in order to be able to recognize responses on a single
// underlying data channel.
func newWebRTCClientStream(
ctx context.Context,
channel *webrtcClientChannel,
stream *webrtcpb.Stream,
onDone func(id uint64),
logger golog.Logger,
) *webrtcClientStream {
ctx, cancel := utils.MergeContext(channel.ctx, ctx)
bs := newWebRTCBaseStream(ctx, cancel, stream, onDone, logger)
s := &webrtcClientStream{
webrtcBaseStream: bs,
ctx: ctx,
cancel: cancel,
ch: channel,
headersReceived: make(chan struct{}),
}
channel.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer channel.activeBackgroundWorkers.Done()
<-ctx.Done()
if !s.webrtcBaseStream.Closed() {
if err := s.resetStream(); err != nil && !errors.Is(err, io.ErrClosedPipe) {
s.webrtcBaseStream.logger.Errorw("error resetting stream", "error", err)
}
}
})
return s
}
// SendMsg is generally called by generated code. On error, SendMsg aborts
// the stream. If the error was generated by the client, the status is
// returned directly; otherwise, io.EOF is returned and the status of
// the stream may be discovered using RecvMsg.
//
// SendMsg blocks until:
// - There is sufficient flow control to schedule m with the transport, or
// - The stream is done, or
// - The stream breaks.
//
// SendMsg does not wait until the message is received by the server. An
// untimely stream closure may result in lost messages. To ensure delivery,
// users should ensure the RPC completed successfully using RecvMsg.
//
// It is safe to have a goroutine calling SendMsg and another goroutine
// calling RecvMsg on the same stream at the same time, but it is undefined behavior
// to call SendMsg on the same stream in different goroutines.
func (s *webrtcClientStream) SendMsg(m interface{}) error {
return s.writeMessage(m, false)
}
// Context returns the context for this stream.
//
// It should not be called until after Header or RecvMsg has returned. Once
// called, subsequent client-side retries are disabled.
func (s *webrtcClientStream) Context() context.Context {
s.webrtcBaseStream.mu.Lock()
defer s.webrtcBaseStream.mu.Unlock()
if s.userCtx == nil {
// be nice to misbehaving users
return s.ctx
}
return s.userCtx
}
// Header returns the header metadata received from the server if there
// is any. It blocks if the metadata is not ready to read.
func (s *webrtcClientStream) Header() (metadata.MD, error) {
select {
case <-s.headersReceived:
return s.headers, nil
default:
}
select {
case <-s.ctx.Done():
return nil, s.ctx.Err()
case <-s.headersReceived:
return s.headers, nil
}
}
// Trailer returns the trailer metadata from the server, if there is any.
// It must only be called after stream.CloseAndRecv has returned, or
// stream.Recv has returned a non-nil error (including io.EOF).
func (s *webrtcClientStream) Trailer() metadata.MD {
s.webrtcBaseStream.mu.Lock()
defer s.webrtcBaseStream.mu.Unlock()
return s.trailers
}
// CloseSend closes the send direction of the stream. It closes the stream
// when non-nil error is met. It is also not safe to call CloseSend
// concurrently with SendMsg.
func (s *webrtcClientStream) CloseSend() error {
return s.writeMessage(nil, true)
}
// checkWriteErrForStreamClose checks the given error to consider the stream for closure.
func checkWriteErrForStreamClose(err error) error {
if err == nil || errors.Is(err, io.ErrClosedPipe) {
// ignore because either no error or we expect to be closed down elsewhere
// in the near future.
return nil
}
return err
}
// resetStream cancels the stream and sends a reset signal.
// It is also not safe to call concurrently with SendMsg.
func (s *webrtcClientStream) resetStream() (err error) {
s.webrtcBaseStream.mu.Lock()
defer s.webrtcBaseStream.mu.Unlock()
if s.sendClosed {
// no need to reset an already closed stream
return nil
}
s.sendClosed = true
defer func() {
s.webrtcBaseStream.closeWithError(checkWriteErrForStreamClose(err), false)
}()
return s.ch.writeReset(s.webrtcBaseStream.stream)
}
func (s *webrtcClientStream) Close() {
s.mu.Lock()
defer s.mu.Unlock()
s.close()
}
func (s *webrtcClientStream) close() {
s.cancel()
s.webrtcBaseStream.close()
}
// writeHeaders is assumed to be called by the client channel in a single goroutine not
// overlapping with any other write.
func (s *webrtcClientStream) writeHeaders(headers *webrtcpb.RequestHeaders) (err error) {
s.mu.Lock()
defer s.mu.Unlock()
defer func() {
if err := checkWriteErrForStreamClose(err); err != nil {
s.webrtcBaseStream.closeWithError(err, false)
}
}()
return s.ch.writeHeaders(s.webrtcBaseStream.stream, headers)
}
var maxRequestMessagePacketDataSize int
func init() {
md, err := proto.Marshal(&webrtcpb.Request{
Stream: &webrtcpb.Stream{
Id: math.MaxUint64,
},
Type: &webrtcpb.Request_Message{
Message: &webrtcpb.RequestMessage{
HasMessage: true,
PacketMessage: &webrtcpb.PacketMessage{
Data: []byte{0x0},
Eom: true,
},
Eos: true,
},
},
})
if err != nil {
panic(err)
}
// maxRequestMessagePacketDataSize = maxDataChannelSize - max proto request wrapper size
maxRequestMessagePacketDataSize = maxDataChannelSize - len(md)
}
func (s *webrtcClientStream) writeMessage(m interface{}, eos bool) (err error) {
s.webrtcBaseStream.mu.RLock()
if s.sendClosed {
s.webrtcBaseStream.mu.RUnlock()
return io.ErrClosedPipe
}
if eos {
s.webrtcBaseStream.mu.RUnlock()
s.webrtcBaseStream.mu.Lock()
if s.sendClosed {
s.webrtcBaseStream.mu.Unlock()
return io.ErrClosedPipe
}
s.sendClosed = true
defer s.webrtcBaseStream.mu.Unlock()
} else {
defer s.webrtcBaseStream.mu.RUnlock()
}
defer func() {
if err := checkWriteErrForStreamClose(err); err != nil {
s.webrtcBaseStream.closeWithError(err, false)
}
}()
var data []byte
if m != nil {
if v1Msg, ok := m.(protov1.Message); ok {
m = protov1.MessageV2(v1Msg)
}
data, err = proto.Marshal(m.(proto.Message))
if err != nil {
return
}
}
if len(data) == 0 {
return s.ch.writeMessage(s.webrtcBaseStream.stream, &webrtcpb.RequestMessage{
HasMessage: m != nil, // maybe no data but a non-nil message
PacketMessage: &webrtcpb.PacketMessage{
Eom: true,
},
Eos: eos,
})
}
for len(data) != 0 {
amountToSend := maxRequestMessagePacketDataSize
if len(data) < amountToSend {
amountToSend = len(data)
}
packet := &webrtcpb.PacketMessage{
Data: data[:amountToSend],
}
data = data[amountToSend:]
if len(data) == 0 {
packet.Eom = true
}
if err := s.ch.writeMessage(s.webrtcBaseStream.stream, &webrtcpb.RequestMessage{
HasMessage: m != nil, // maybe no data but a non-nil message
PacketMessage: packet,
Eos: eos,
}); err != nil {
return err
}
}
return nil
}
func (s *webrtcClientStream) onResponse(resp *webrtcpb.Response) {
switch r := resp.Type.(type) {
case *webrtcpb.Response_Headers:
select {
case <-s.headersReceived:
s.webrtcBaseStream.closeWithError(errors.New("headers already received"), false)
return
default:
}
if s.trailersReceived {
s.webrtcBaseStream.closeWithError(errors.New("headers received after trailers"), false)
return
}
s.processHeaders(r.Headers)
case *webrtcpb.Response_Message:
select {
case <-s.headersReceived:
default:
s.webrtcBaseStream.closeWithError(errors.New("headers not yet received"), false)
return
}
if s.trailersReceived {
s.webrtcBaseStream.closeWithError(errors.New("message received after trailers"), false)
return
}
s.processMessage(r.Message)
case *webrtcpb.Response_Trailers:
s.processTrailers(r.Trailers)
default:
s.webrtcBaseStream.logger.Errorf("unknown response type %T", r)
}
}
func (s *webrtcClientStream) processHeaders(headers *webrtcpb.ResponseHeaders) {
s.webrtcBaseStream.mu.Lock()
s.headers = metadataFromProto(headers.Metadata)
s.userCtx = metadata.NewIncomingContext(s.ctx, s.headers)
s.webrtcBaseStream.mu.Unlock()
close(s.headersReceived)
}
func (s *webrtcClientStream) processMessage(msg *webrtcpb.ResponseMessage) {
if s.trailersReceived {
s.webrtcBaseStream.logger.Error("message received after trailers")
return
}
data, eop := s.webrtcBaseStream.processMessage(msg.PacketMessage)
if !eop {
return
}
s.webrtcBaseStream.mu.Lock()
if s.webrtcBaseStream.recvClosed.Load() {
s.webrtcBaseStream.mu.Unlock()
return
}
msgCh := s.webrtcBaseStream.msgCh
s.webrtcBaseStream.activeSenders.Add(1)
s.webrtcBaseStream.mu.Unlock()
func() {
defer s.webrtcBaseStream.activeSenders.Done()
select {
case msgCh <- data:
case <-s.ctx.Done():
}
}()
}
func (s *webrtcClientStream) processTrailers(trailers *webrtcpb.ResponseTrailers) {
s.webrtcBaseStream.mu.Lock()
defer s.webrtcBaseStream.mu.Unlock()
s.trailersReceived = true
if trailers.Metadata != nil {
s.trailers = metadataFromProto(trailers.Metadata)
}
respStatus := status.FromProto(trailers.Status)
s.webrtcBaseStream.closeFromTrailers(respStatus.Err())
}