Merge pull request #645 from iamqizhao/master
Phase 2 for server interceptor: The installation of server unary interceptor
This commit is contained in:
15
server.go
15
server.go
@ -99,6 +99,7 @@ type options struct {
|
|||||||
codec Codec
|
codec Codec
|
||||||
cp Compressor
|
cp Compressor
|
||||||
dc Decompressor
|
dc Decompressor
|
||||||
|
unaryInt UnaryServerInterceptor
|
||||||
maxConcurrentStreams uint32
|
maxConcurrentStreams uint32
|
||||||
useHandlerImpl bool // use http.Handler-based server
|
useHandlerImpl bool // use http.Handler-based server
|
||||||
}
|
}
|
||||||
@ -140,6 +141,18 @@ func Creds(c credentials.Credentials) ServerOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the
|
||||||
|
// server. Only one interceptor can be installed. The construction of multiple interceptors
|
||||||
|
// (e.g., chaining) can be implemented at the caller.
|
||||||
|
func UnaryInterceptor(i UnaryServerInterceptor) ServerOption {
|
||||||
|
return func(o *options) {
|
||||||
|
if o.unaryInt != nil {
|
||||||
|
panic("The unary server interceptor has been set.")
|
||||||
|
}
|
||||||
|
o.unaryInt = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewServer creates a gRPC server which has no service registered and has not
|
// NewServer creates a gRPC server which has no service registered and has not
|
||||||
// started to accept requests yet.
|
// started to accept requests yet.
|
||||||
func NewServer(opt ...ServerOption) *Server {
|
func NewServer(opt ...ServerOption) *Server {
|
||||||
@ -494,7 +507,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport.
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
reply, appErr := md.Handler(srv.server, stream.Context(), df, nil)
|
reply, appErr := md.Handler(srv.server, stream.Context(), df, s.opts.unaryInt)
|
||||||
if appErr != nil {
|
if appErr != nil {
|
||||||
if err, ok := appErr.(rpcError); ok {
|
if err, ok := appErr.(rpcError); ok {
|
||||||
statusCode = err.code
|
statusCode = err.code
|
||||||
|
@ -420,6 +420,7 @@ type test struct {
|
|||||||
userAgent string
|
userAgent string
|
||||||
clientCompression bool
|
clientCompression bool
|
||||||
serverCompression bool
|
serverCompression bool
|
||||||
|
unaryInt grpc.UnaryServerInterceptor
|
||||||
|
|
||||||
// srv and srvAddr are set once startServer is called.
|
// srv and srvAddr are set once startServer is called.
|
||||||
srv *grpc.Server
|
srv *grpc.Server
|
||||||
@ -468,7 +469,9 @@ func (te *test) startServer() {
|
|||||||
grpc.RPCDecompressor(grpc.NewGZIPDecompressor()),
|
grpc.RPCDecompressor(grpc.NewGZIPDecompressor()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
if te.unaryInt != nil {
|
||||||
|
sopts = append(sopts, grpc.UnaryInterceptor(te.unaryInt))
|
||||||
|
}
|
||||||
la := "localhost:0"
|
la := "localhost:0"
|
||||||
switch e.network {
|
switch e.network {
|
||||||
case "unix":
|
case "unix":
|
||||||
@ -1685,6 +1688,29 @@ func testCompressOK(t *testing.T, e env) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnaryServerInterceptor(t *testing.T) {
|
||||||
|
defer leakCheck(t)()
|
||||||
|
for _, e := range listTestEnv() {
|
||||||
|
testUnaryServerInterceptor(t, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func errInjector(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||||
|
return nil, grpc.Errorf(codes.PermissionDenied, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUnaryServerInterceptor(t *testing.T, e env) {
|
||||||
|
te := newTest(t, e)
|
||||||
|
te.unaryInt = errInjector
|
||||||
|
te.startServer()
|
||||||
|
defer te.tearDown()
|
||||||
|
|
||||||
|
tc := testpb.NewTestServiceClient(te.clientConn())
|
||||||
|
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); grpc.Code(err) != codes.PermissionDenied {
|
||||||
|
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, error code %d", err, codes.PermissionDenied)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// funcServer implements methods of TestServiceServer using funcs,
|
// funcServer implements methods of TestServiceServer using funcs,
|
||||||
// similar to an http.HandlerFunc.
|
// similar to an http.HandlerFunc.
|
||||||
// Any unimplemented method will crash. Tests implement the method(s)
|
// Any unimplemented method will crash. Tests implement the method(s)
|
||||||
|
Reference in New Issue
Block a user