-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_client.go
75 lines (60 loc) · 2.02 KB
/
stream_client.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
package grpc_interceptor
import (
"context"
"google.golang.org/grpc"
)
type streamClientInterceptorGroup struct {
ics []grpc.StreamClientInterceptor
domain domain
}
type StreamClientInterceptors struct {
s []streamClientInterceptorGroup
}
func (sci *StreamClientInterceptors) Add(interceptors ...grpc.StreamClientInterceptor) *StreamClientInterceptors {
sci.s = append(sci.s, streamClientInterceptorGroup{
ics: interceptors,
domain: newDomain(),
})
return sci
}
func (sci *StreamClientInterceptors) AddWithoutMethods(methods []string, interceptors ...grpc.StreamClientInterceptor) *StreamClientInterceptors {
sci.s = append(sci.s, streamClientInterceptorGroup{
ics: interceptors,
domain: newBlackDomain(methods),
})
return sci
}
func (sci *StreamClientInterceptors) AddOnMethods(methods []string, interceptors ...grpc.StreamClientInterceptor) *StreamClientInterceptors {
if len(methods) > 0 {
sci.s = append(sci.s, streamClientInterceptorGroup{
ics: interceptors,
domain: newWhiteDomain(methods),
})
}
return sci
}
func (sci *StreamClientInterceptors) 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) {
if len(sci.s) == 0 {
return streamer(ctx, desc, cc, method, opts...)
}
var cursor handleCursor
var chainHandler grpc.Streamer
chainHandler = func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
opts ...grpc.CallOption) (grpc.ClientStream, error) {
for cursor.segment < len(sci.s) {
group := sci.s[cursor.segment]
if group.domain.isOnMethod(method) && cursor.offset < len(group.ics) {
ic := group.ics[cursor.offset]
cursor.offset++
return ic(ctx, desc, cc, method, chainHandler, opts...)
}
cursor.offset = 0
cursor.segment++
}
return streamer(ctx, desc, cc, method, opts...)
}
return chainHandler(ctx, desc, cc, method, opts...)
}
}