-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterceptor.go
More file actions
44 lines (36 loc) · 1.1 KB
/
interceptor.go
File metadata and controls
44 lines (36 loc) · 1.1 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
package mrpc
import (
"context"
)
// CallInfo holds details about a method call on the server side.
type CallInfo struct {
Service interface{}
Method string
Body []byte
}
// ServerInterceptor defines a function type for intercepting a request on
// the server side. The interceptor is responsible to call h to complete the
// method call.
type ServerInterceptor func(ctx context.Context, call CallInfo, h Handler) ([]byte, error)
func serverInterceptorChain(interceptors []ServerInterceptor) ServerInterceptor {
switch len(interceptors) {
case 0:
return func(ctx context.Context, call CallInfo, h Handler) ([]byte, error) {
return h(ctx, call.Service, call.Body)
}
case 1:
return interceptors[0]
default:
return func(ctx context.Context, call CallInfo, h Handler) ([]byte, error) {
var next Handler
idx := 0
next = func(ctx context.Context, svc interface{}, body []byte) ([]byte, error) {
if idx++; idx == len(interceptors) {
return h(ctx, call.Service, call.Body)
}
return interceptors[idx](ctx, call, next)
}
return interceptors[idx](ctx, call, next)
}
}
}