-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterceptor.go
More file actions
212 lines (191 loc) · 8 KB
/
interceptor.go
File metadata and controls
212 lines (191 loc) · 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
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
package srpc
import (
"context"
"google.golang.org/grpc"
)
// Interceptor manages the RPC interceptor chains for both server and client-side
// interceptors, supporting both unary and streaming modes. It provides chainable
// methods to add multiple interceptors that will be executed in sequence.
//
// Interceptors follow an "onion model" execution pattern:
// - For servers: The first added interceptor processes the request first and the response last
// - For clients: The first added interceptor processes outgoing messages first and incoming messages last
//
// Request flow visualization:
//
// client.Send() client.Receive()
// | ^
// v |
// A --- --- A
// B --- --- B
// : ... ... :
// Y --- --- Y
// Z --- --- Z
// | ^
// v |
// = = = = = = = = = = = = = = = =
// network
// = = = = = = = = = = = = = = = =
// | ^
// v |
// A --- --- A
// B --- --- B
// : ... ... :
// Y --- --- Y
// Z --- --- Z
// | ^
// v |
// handler.Receive() handler.Send()
// | ^
// | |
// '-> handler logic >-'
//
// Note: In clients, Send handles request messages and Receive handles response messages.
// For handlers, it's the reverse. Depending on your interceptor's logic, you may need
// to wrap different methods in clients versus servers.
//
// Interceptors are commonly used for implementing cross-cutting concerns like logging,
// authentication, error handling, metrics collection, and tracing.
type Interceptor struct {
chainUnaryServerInts []UnaryServerInterceptor
chainStreamServerInts []StreamServerInterceptor
chainUnaryClientInts []UnaryClientInterceptor
chainStreamClientInts []StreamClientInterceptor
}
type UnaryHandler = grpc.UnaryHandler
type UnaryServerInfo = grpc.UnaryServerInfo
type UnaryServerInterceptor = grpc.UnaryServerInterceptor
// ChainUnaryInterceptor adds unary server interceptors to the chain.
// See [grpc.ChainUnaryInterceptor] for more details.
func (i *Interceptor) ChainUnaryInterceptor(ints ...UnaryServerInterceptor) *Interceptor {
i.chainUnaryServerInts = append(i.chainUnaryServerInts, ints...)
return i
}
func (i *Interceptor) UnaryInterceptor() UnaryServerInterceptor {
interceptors := i.chainUnaryServerInts
var chainedInt UnaryServerInterceptor
if len(interceptors) == 0 {
chainedInt = nil
} else if len(interceptors) == 1 {
chainedInt = interceptors[0]
} else {
chainedInt = chainUnaryInterceptors(interceptors)
}
return chainedInt
}
func chainUnaryInterceptors(interceptors []UnaryServerInterceptor) UnaryServerInterceptor {
return func(ctx context.Context, req any, info *UnaryServerInfo, handler UnaryHandler) (any, error) {
return interceptors[0](ctx, req, info, getChainUnaryHandler(interceptors, 0, info, handler))
}
}
func getChainUnaryHandler(interceptors []UnaryServerInterceptor, curr int, info *UnaryServerInfo, finalHandler UnaryHandler) UnaryHandler {
if curr == len(interceptors)-1 {
return finalHandler
}
return func(ctx context.Context, req any) (any, error) {
return interceptors[curr+1](ctx, req, info, getChainUnaryHandler(interceptors, curr+1, info, finalHandler))
}
}
type ServerStream = grpc.ServerStream
type StreamHandler = grpc.StreamHandler
type StreamServerInfo = grpc.StreamServerInfo
type StreamServerInterceptor = grpc.StreamServerInterceptor
// ChainStreamInterceptor adds stream server interceptors to the chain.
// See [grpc.ChainStreamInterceptor] for more details.
func (i *Interceptor) ChainStreamInterceptor(ints ...StreamServerInterceptor) *Interceptor {
i.chainStreamServerInts = append(i.chainStreamServerInts, ints...)
return i
}
func (i *Interceptor) StreamInterceptor() StreamServerInterceptor {
interceptors := i.chainStreamServerInts
var chainedInt StreamServerInterceptor
if len(interceptors) == 0 {
chainedInt = nil
} else if len(interceptors) == 1 {
chainedInt = interceptors[0]
} else {
chainedInt = chainStreamInterceptors(interceptors)
}
return chainedInt
}
func chainStreamInterceptors(interceptors []StreamServerInterceptor) StreamServerInterceptor {
return func(srv any, ss ServerStream, info *StreamServerInfo, handler StreamHandler) error {
// Start the chain with the first interceptor
return interceptors[0](srv, ss, info, getChainStreamHandler(interceptors, 0, info, handler))
}
}
func getChainStreamHandler(interceptors []StreamServerInterceptor, curr int, info *StreamServerInfo, finalHandler StreamHandler) StreamHandler {
if curr == len(interceptors)-1 {
// If it's the last interceptor, call the final handler
return finalHandler
}
return func(srv any, stream ServerStream) error {
// Call the next interceptor in the chain
return interceptors[curr+1](srv, stream, info, getChainStreamHandler(interceptors, curr+1, info, finalHandler))
}
}
type UnaryClientInterceptor = grpc.UnaryClientInterceptor
type UnaryInvoker = grpc.UnaryInvoker
type CallOption = grpc.CallOption
type ClientConn = grpc.ClientConn
// WithChainUnaryInterceptor adds unary client interceptors to the chain.
// See [WithChainUnaryInterceptor] for more details.
func (i *Interceptor) WithChainUnaryInterceptor(ints ...UnaryClientInterceptor) *Interceptor {
i.chainUnaryClientInts = append(i.chainUnaryClientInts, ints...)
return i
}
func (i *Interceptor) UnaryClientInterceptor() UnaryClientInterceptor {
interceptors := i.chainUnaryClientInts
var chainedInt UnaryClientInterceptor
if len(interceptors) == 0 {
chainedInt = nil
} else if len(interceptors) == 1 {
chainedInt = interceptors[0]
} else {
chainedInt = func(ctx context.Context, method string, req, reply any, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
return interceptors[0](ctx, method, req, reply, cc, getChainUnaryInvoker(interceptors, 0, invoker), opts...)
}
}
return chainedInt
}
func getChainUnaryInvoker(interceptors []UnaryClientInterceptor, curr int, finalInvoker UnaryInvoker) UnaryInvoker {
if curr == len(interceptors)-1 {
return finalInvoker
}
return func(ctx context.Context, method string, req, reply any, cc *ClientConn, opts ...CallOption) error {
return interceptors[curr+1](ctx, method, req, reply, cc, getChainUnaryInvoker(interceptors, curr+1, finalInvoker), opts...)
}
}
type StreamClientInterceptor = grpc.StreamClientInterceptor
type ClientStream = grpc.ClientStream
type Streamer = grpc.Streamer
type GRPCStreamDesc = grpc.StreamDesc
// WithChainStreamInterceptor adds stream client interceptors to the chain.
// See [grpc.WithChainStreamInterceptor] for more details.
func (i *Interceptor) WithChainStreamInterceptor(ints ...StreamClientInterceptor) *Interceptor {
i.chainStreamClientInts = append(i.chainStreamClientInts, ints...)
return i
}
func (i *Interceptor) StreamClientInterceptor() StreamClientInterceptor {
interceptors := i.chainStreamClientInts
var chainedInt StreamClientInterceptor
if len(interceptors) == 0 {
chainedInt = nil
} else if len(interceptors) == 1 {
chainedInt = interceptors[0]
} else {
chainedInt = func(ctx context.Context, desc *GRPCStreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error) {
return interceptors[0](ctx, desc, cc, method, getChainStreamer(interceptors, 0, streamer), opts...)
}
}
return chainedInt
}
// getChainStreamer recursively generate the chained client stream constructor.
func getChainStreamer(interceptors []StreamClientInterceptor, curr int, finalStreamer Streamer) Streamer {
if curr == len(interceptors)-1 {
return finalStreamer
}
return func(ctx context.Context, desc *GRPCStreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
return interceptors[curr+1](ctx, desc, cc, method, getChainStreamer(interceptors, curr+1, finalStreamer), opts...)
}
}