-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_server.go
More file actions
71 lines (58 loc) · 1.8 KB
/
stream_server.go
File metadata and controls
71 lines (58 loc) · 1.8 KB
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
package grpc_interceptor
import (
"google.golang.org/grpc"
)
type streamServerInterceptorGroup struct {
ics []grpc.StreamServerInterceptor
domain domain
}
type StreamServerInterceptors struct {
s []streamServerInterceptorGroup
}
func (ssi *StreamServerInterceptors) Add(interceptors ...grpc.StreamServerInterceptor) *StreamServerInterceptors {
ssi.s = append(ssi.s, streamServerInterceptorGroup{
ics: interceptors,
domain: newDomain(),
})
return ssi
}
func (ssi *StreamServerInterceptors) AddWithoutMethods(methods []string, interceptors ...grpc.StreamServerInterceptor) *StreamServerInterceptors {
ssi.s = append(ssi.s, streamServerInterceptorGroup{
ics: interceptors,
domain: newBlackDomain(methods),
})
return ssi
}
func (ssi *StreamServerInterceptors) AddOnMethods(methods []string, interceptors ...grpc.StreamServerInterceptor) *StreamServerInterceptors {
if len(methods) > 0 {
ssi.s = append(ssi.s, streamServerInterceptorGroup{
ics: interceptors,
domain: newWhiteDomain(methods),
})
}
return ssi
}
func (ssi *StreamServerInterceptors) StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo,
handler grpc.StreamHandler) error {
if len(ssi.s) == 0 {
return handler(srv, ss)
}
var cursor handleCursor
var chainHandler grpc.StreamHandler
chainHandler = func(srv interface{}, ss grpc.ServerStream) error {
for cursor.segment < len(ssi.s) {
group := ssi.s[cursor.segment]
if group.domain.isOnMethod(info.FullMethod) && cursor.offset < len(group.ics) {
ic := group.ics[cursor.offset]
cursor.offset++
return ic(srv, ss, info, chainHandler)
}
cursor.offset = 0
cursor.segment++
}
return handler(srv, ss)
}
return chainHandler(srv, ss)
}
}