-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptors.go
21 lines (16 loc) · 912 Bytes
/
interceptors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package rpc
// ClientInterceptor is a function type that represents an interceptor for client requests.
// It takes a pointer to a Request struct and returns a pointer to a Response struct and an error.
type RequestHandler func(req *Request) (*Response, error)
type Interceptor func(next RequestHandler) RequestHandler
// useInterceptors applies a list of interceptors to a given RequestHandler.
// The interceptors are applied in reverse order, starting from the last interceptor in the slice.
// Each interceptor is called with the current RequestHandler as an argument, and the returned
// RequestHandler becomes the input for the next interceptor. The final RequestHandler is then
// returned as the result.
func useInterceptors(handler RequestHandler, interceptors []Interceptor) RequestHandler {
for i := len(interceptors) - 1; i >= 0; i-- {
handler = interceptors[i](handler)
}
return handler
}