-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper_stream.go
58 lines (50 loc) · 2.2 KB
/
wrapper_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
package client
import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type ClientStreamWithContext struct {
cs grpc.ClientStream
// ctx is the Context which we can assign it.
ctx context.Context
}
func (w *ClientStreamWithContext) SetContext(ctx context.Context) {
w.ctx = ctx
}
// Stream interface
func (w *ClientStreamWithContext) Context() context.Context { return w.ctx }
func (w *ClientStreamWithContext) RecvMsg(msg interface{}) error { return w.cs.RecvMsg(msg) }
func (w *ClientStreamWithContext) SendMsg(msg interface{}) error { return w.cs.SendMsg(msg) }
// ClientStream interface
func (w *ClientStreamWithContext) Header() (metadata.MD, error) { return w.cs.Header() }
func (w *ClientStreamWithContext) Trailer() metadata.MD { return w.cs.Trailer() }
func (w *ClientStreamWithContext) CloseSend() error { return w.cs.CloseSend() }
// WrapClientStreamWithContext returns a ClientStream that has the ability to overwrite context.
func WrapClientStreamWithContext(stream grpc.ClientStream) *ClientStreamWithContext {
exists, ok := stream.(*ClientStreamWithContext)
if ok {
return exists
}
return &ClientStreamWithContext{cs: stream, ctx: stream.Context()}
}
// WrapperStreamClient creates a single interceptor out of a chain of many interceptors
// Execution is done in right-to-left order
func WrapperStreamClient(interceptors ...grpc.StreamClientInterceptor) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
h := wrapStream(streamer, interceptors...)
return h(ctx, desc, cc, method, opts...)
}
}
// wrap h with all specified interceptors
func wrapStream(ui grpc.Streamer, interceptors ...grpc.StreamClientInterceptor) grpc.Streamer {
for _, i := range interceptors {
h := func(current grpc.StreamClientInterceptor, next grpc.Streamer) grpc.Streamer {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return current(ctx, desc, cc, method, next, opts...)
}
}
ui = h(i, ui)
}
return ui
}