2019-07-30 13:26:48 +00:00
|
|
|
// Copyright 2016 Michal Witkowski. All Rights Reserved.
|
|
|
|
// See LICENSE for licensing terms.
|
|
|
|
|
|
|
|
// gRPC Server Interceptor chaining middleware.
|
|
|
|
|
|
|
|
package grpc_middleware
|
|
|
|
|
|
|
|
import (
|
2019-09-20 10:45:13 +00:00
|
|
|
"context"
|
|
|
|
|
2019-07-30 13:26:48 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChainUnaryServer creates a single interceptor out of a chain of many interceptors.
|
|
|
|
//
|
|
|
|
// Execution is done in left-to-right order, including passing of context.
|
|
|
|
// For example ChainUnaryServer(one, two, three) will execute one before two before three, and three
|
|
|
|
// will see context changes of one and two.
|
|
|
|
func ChainUnaryServer(interceptors ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
|
|
|
|
n := len(interceptors)
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
chainer := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler {
|
|
|
|
return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) {
|
|
|
|
return currentInter(currentCtx, currentReq, info, currentHandler)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
chainedHandler := handler
|
|
|
|
for i := n - 1; i >= 0; i-- {
|
|
|
|
chainedHandler = chainer(interceptors[i], chainedHandler)
|
|
|
|
}
|
2019-07-30 13:26:48 +00:00
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return chainedHandler(ctx, req)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChainStreamServer creates a single interceptor out of a chain of many interceptors.
|
|
|
|
//
|
|
|
|
// Execution is done in left-to-right order, including passing of context.
|
|
|
|
// For example ChainUnaryServer(one, two, three) will execute one before two before three.
|
|
|
|
// If you want to pass context between interceptors, use WrapServerStream.
|
|
|
|
func ChainStreamServer(interceptors ...grpc.StreamServerInterceptor) grpc.StreamServerInterceptor {
|
|
|
|
n := len(interceptors)
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
|
|
chainer := func(currentInter grpc.StreamServerInterceptor, currentHandler grpc.StreamHandler) grpc.StreamHandler {
|
|
|
|
return func(currentSrv interface{}, currentStream grpc.ServerStream) error {
|
|
|
|
return currentInter(currentSrv, currentStream, info, currentHandler)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
chainedHandler := handler
|
|
|
|
for i := n - 1; i >= 0; i-- {
|
|
|
|
chainedHandler = chainer(interceptors[i], chainedHandler)
|
|
|
|
}
|
2019-07-30 13:26:48 +00:00
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return chainedHandler(srv, ss)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChainUnaryClient creates a single interceptor out of a chain of many interceptors.
|
|
|
|
//
|
|
|
|
// Execution is done in left-to-right order, including passing of context.
|
|
|
|
// For example ChainUnaryClient(one, two, three) will execute one before two before three.
|
|
|
|
func ChainUnaryClient(interceptors ...grpc.UnaryClientInterceptor) grpc.UnaryClientInterceptor {
|
|
|
|
n := len(interceptors)
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
|
|
chainer := func(currentInter grpc.UnaryClientInterceptor, currentInvoker grpc.UnaryInvoker) grpc.UnaryInvoker {
|
|
|
|
return func(currentCtx context.Context, currentMethod string, currentReq, currentRepl interface{}, currentConn *grpc.ClientConn, currentOpts ...grpc.CallOption) error {
|
|
|
|
return currentInter(currentCtx, currentMethod, currentReq, currentRepl, currentConn, currentInvoker, currentOpts...)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
chainedInvoker := invoker
|
|
|
|
for i := n - 1; i >= 0; i-- {
|
|
|
|
chainedInvoker = chainer(interceptors[i], chainedInvoker)
|
|
|
|
}
|
2019-07-30 13:26:48 +00:00
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return chainedInvoker(ctx, method, req, reply, cc, opts...)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChainStreamClient creates a single interceptor out of a chain of many interceptors.
|
|
|
|
//
|
|
|
|
// Execution is done in left-to-right order, including passing of context.
|
|
|
|
// For example ChainStreamClient(one, two, three) will execute one before two before three.
|
|
|
|
func ChainStreamClient(interceptors ...grpc.StreamClientInterceptor) grpc.StreamClientInterceptor {
|
|
|
|
n := len(interceptors)
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
|
|
|
chainer := func(currentInter grpc.StreamClientInterceptor, currentStreamer grpc.Streamer) grpc.Streamer {
|
|
|
|
return func(currentCtx context.Context, currentDesc *grpc.StreamDesc, currentConn *grpc.ClientConn, currentMethod string, currentOpts ...grpc.CallOption) (grpc.ClientStream, error) {
|
|
|
|
return currentInter(currentCtx, currentDesc, currentConn, currentMethod, currentStreamer, currentOpts...)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
chainedStreamer := streamer
|
|
|
|
for i := n - 1; i >= 0; i-- {
|
|
|
|
chainedStreamer = chainer(interceptors[i], chainedStreamer)
|
|
|
|
}
|
2019-07-30 13:26:48 +00:00
|
|
|
|
2019-09-20 10:45:13 +00:00
|
|
|
return chainedStreamer(ctx, desc, cc, method, opts...)
|
2019-07-30 13:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chain creates a single interceptor out of a chain of many interceptors.
|
|
|
|
//
|
|
|
|
// WithUnaryServerChain is a grpc.Server config option that accepts multiple unary interceptors.
|
|
|
|
// Basically syntactic sugar.
|
|
|
|
func WithUnaryServerChain(interceptors ...grpc.UnaryServerInterceptor) grpc.ServerOption {
|
|
|
|
return grpc.UnaryInterceptor(ChainUnaryServer(interceptors...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStreamServerChain is a grpc.Server config option that accepts multiple stream interceptors.
|
|
|
|
// Basically syntactic sugar.
|
|
|
|
func WithStreamServerChain(interceptors ...grpc.StreamServerInterceptor) grpc.ServerOption {
|
|
|
|
return grpc.StreamInterceptor(ChainStreamServer(interceptors...))
|
|
|
|
}
|