-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterceptor.go
More file actions
49 lines (41 loc) · 1.66 KB
/
interceptor.go
File metadata and controls
49 lines (41 loc) · 1.66 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
package ngrpc
import (
"context"
"google.golang.org/grpc"
)
// GrpcContextHandler ...
type GrpcContextHandler func(ctx context.Context) context.Context
// UnaryServerInterceptor ...
func UnaryServerInterceptor(f GrpcContextHandler) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
ctx = f(ctx)
return handler(ctx, req)
}
}
// StreamServerInterceptor ...
func StreamServerInterceptor(f GrpcContextHandler) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
wrapped := WrapServerStream(stream)
wrapped.WrappedContext = f(stream.Context())
return handler(srv, wrapped)
}
}
// WrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context.
// Copied from github.com/grpc-ecosystem/go-grpc-middleware/v2
type WrappedServerStream struct {
grpc.ServerStream
// WrappedContext is the wrapper's own Context. You can assign it.
WrappedContext context.Context
}
// Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context()
func (w *WrappedServerStream) Context() context.Context {
return w.WrappedContext
}
// WrapServerStream returns a ServerStream that has the ability to overwrite context.
// Copied from github.com/grpc-ecosystem/go-grpc-middleware/v2
func WrapServerStream(stream grpc.ServerStream) *WrappedServerStream {
if existing, ok := stream.(*WrappedServerStream); ok {
return existing
}
return &WrappedServerStream{ServerStream: stream, WrappedContext: stream.Context()}
}