-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhandler_one2many.go
324 lines (251 loc) · 8.05 KB
/
handler_one2many.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
// Copyright 2019 Andrey Smirnov. All Rights Reserved.
// See LICENSE for licensing terms.
package proxy
import (
"context"
"errors"
"fmt"
"io"
"github.com/hashicorp/go-multierror"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *handler) handlerOne2Many(fullMethodName string, serverStream grpc.ServerStream, backendConnections []backendConnection) error {
// wrap the stream for safe concurrent access
serverStream = &ServerStreamWrapper{ServerStream: serverStream}
s2cErrChan := s.forwardServerToClientsMulti(serverStream, backendConnections)
var c2sErrChan chan error
if s.options.streamedDetector != nil && s.options.streamedDetector(fullMethodName) {
c2sErrChan = s.forwardClientsToServerMultiStreaming(backendConnections, serverStream)
} else {
c2sErrChan = s.forwardClientsToServerMultiUnary(backendConnections, serverStream)
}
for range 2 {
select {
case s2cErr := <-s2cErrChan:
if errors.Is(s2cErr, io.EOF) {
// this is the happy case where the sender has encountered io.EOF, and won't be sending anymore./
// the clientStream>serverStream may continue pumping though.
for i := range backendConnections {
if backendConnections[i].clientStream != nil {
backendConnections[i].clientStream.CloseSend() //nolint: errcheck
}
}
} else {
// however, we may have gotten a receive error (stream disconnected, a read error etc) in which case we need
// to cancel the clientStream to the backend, let all of its goroutines be freed up by the CancelFunc and
// exit with an error to the stack
return status.Errorf(codes.Internal, "failed proxying s2c: %v", s2cErr)
}
case c2sErr := <-c2sErrChan:
// c2sErr will contain RPC error from client code. If not io.EOF return the RPC error as server stream error.
if !errors.Is(c2sErr, io.EOF) {
return c2sErr
}
return nil
}
}
return status.Errorf(codes.Internal, "gRPC proxying should never reach this stage.")
}
// formatError tries to format error from upstream as message to the client.
func (s *handler) formatError(streaming bool, src *backendConnection, backendErr error) ([]byte, error) {
payload, err := src.backend.BuildError(streaming, backendErr)
if err != nil {
return nil, fmt.Errorf("error building error for %s: %w", src.backend, err)
}
if payload == nil {
err = backendErr
}
return payload, err
}
// sendError tries to deliver error back to the client via dst.
//
// If sendError fails to deliver the error, error is returned.
// If sendError successfully delivers the error, nil is returned.
func (s *handler) sendError(src *backendConnection, dst grpc.ServerStream, backendErr error) error {
payload, err := s.formatError(true, src, backendErr)
if err != nil {
return err
}
f := NewFrame(payload)
if err = dst.SendMsg(f); err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
if rpcStatus, ok := status.FromError(err); ok && rpcStatus.Message() == "transport is closing" {
return nil
}
return fmt.Errorf("error sending error back: %w", err)
}
return nil
}
// forwardClientsToServerMultiUnary handles one:many proxying, unary call version (merging results)
//
//nolint:gocognit
func (s *handler) forwardClientsToServerMultiUnary(sources []backendConnection, dst grpc.ServerStream) chan error {
ret := make(chan error, 1)
payloadCh := make(chan []byte, len(sources))
errCh := make(chan error, len(sources))
for i := range sources {
go func(src *backendConnection) {
errCh <- func() error {
if src.connError != nil {
payload, err := s.formatError(false, src, src.connError)
if err != nil {
return err
}
payloadCh <- payload
return nil
}
// Send the header metadata first
md, err := src.clientStream.Header()
if err != nil {
var payload []byte
payload, err = s.formatError(false, src, err)
if err != nil {
return err
}
payloadCh <- payload
return nil
}
if md != nil {
if err = dst.SetHeader(md); err != nil {
return fmt.Errorf("error setting headers from client %s: %w", src.backend, err)
}
}
f := &frame{}
for {
if err := src.clientStream.RecvMsg(f); err != nil {
if errors.Is(err, io.EOF) {
// This happens when the clientStream has nothing else to offer (io.EOF), returned a gRPC error. In those two
// cases we may have received Trailers as part of the call. In case of other errors (stream closed) the trailers
// will be nil.
dst.SetTrailer(src.clientStream.Trailer())
return nil
}
var payload []byte
payload, err = s.formatError(false, src, err)
if err != nil {
return err
}
payloadCh <- payload
return nil
}
var err error
f.payload, err = src.backend.AppendInfo(false, f.payload)
if err != nil {
return fmt.Errorf("error appending info for %s: %w", src.backend, err)
}
payloadCh <- f.payload
}
}()
}(&sources[i])
}
go func() {
var multiErr *multierror.Error
for range sources {
multiErr = multierror.Append(multiErr, <-errCh)
}
if multiErr.ErrorOrNil() != nil {
ret <- multiErr.ErrorOrNil()
return
}
close(payloadCh)
var merged []byte
for b := range payloadCh {
merged = append(merged, b...)
}
ret <- dst.SendMsg(NewFrame(merged))
}()
return ret
}
// one:many proxying, streaming version (no merge).
//
//nolint:gocognit
func (s *handler) forwardClientsToServerMultiStreaming(sources []backendConnection, dst grpc.ServerStream) chan error {
ret := make(chan error, 1)
errCh := make(chan error, len(sources))
for i := range sources {
go func(src *backendConnection) {
errCh <- func() error {
if src.connError != nil {
return s.sendError(src, dst, src.connError)
}
f := &frame{}
for j := 0; ; j++ {
if err := src.clientStream.RecvMsg(f); err != nil {
if errors.Is(err, io.EOF) {
// This happens when the clientStream has nothing else to offer (io.EOF), returned a gRPC error. In those two
// cases we may have received Trailers as part of the call. In case of other errors (stream closed) the trailers
// will be nil.
dst.SetTrailer(src.clientStream.Trailer())
return nil
}
return s.sendError(src, dst, err)
}
if j == 0 {
// This is a bit of a hack, but client to server headers are only readable after first client msg is
// received but must be written to server stream before the first msg is flushed.
// This is the only place to do it nicely.
md, err := src.clientStream.Header()
if err != nil {
return s.sendError(src, dst, err)
}
dst.SetHeader(md) //nolint:errcheck // ignore errors, as we might try to set headers multiple times
}
var err error
f.payload, err = src.backend.AppendInfo(true, f.payload)
if err != nil {
return fmt.Errorf("error appending info for %s: %w", src.backend, err)
}
if err = dst.SendMsg(f); err != nil {
return fmt.Errorf("error sending back to server from %s: %w", src.backend, err)
}
}
}()
}(&sources[i])
}
go func() {
var multiErr *multierror.Error
for range sources {
multiErr = multierror.Append(multiErr, <-errCh)
}
ret <- multiErr.ErrorOrNil()
}()
return ret
}
func (s *handler) forwardServerToClientsMulti(src grpc.ServerStream, destinations []backendConnection) chan error {
ret := make(chan error, 1)
go func() {
f := NewFrame(nil)
for {
if err := src.RecvMsg(f); err != nil {
ret <- err
return
}
errCh := make(chan error)
for i := range destinations {
go func(dst *backendConnection) {
errCh <- func() error {
if dst.clientStream == nil || dst.connError != nil {
return nil //nolint:nilerr // skip it
}
return dst.clientStream.SendMsg(f)
}()
}(&destinations[i])
}
liveDestinations := 0
for range destinations {
if err := <-errCh; err == nil {
liveDestinations++
}
}
if liveDestinations == 0 {
ret <- io.EOF
return
}
}
}()
return ret
}