From c321387fd984d6082a3b5a1d603a18e51ed04212 Mon Sep 17 00:00:00 2001 From: iamqizhao Date: Tue, 19 Apr 2016 15:54:30 -0700 Subject: [PATCH 1/3] the installation of server unary interceptor --- server.go | 12 +++++++++++- test/end2end_test.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index fde4311a..995dcaea 100644 --- a/server.go +++ b/server.go @@ -99,6 +99,7 @@ type options struct { codec Codec cp Compressor dc Decompressor + unaryInt UnaryServerInterceptor maxConcurrentStreams uint32 useHandlerImpl bool // use http.Handler-based server } @@ -140,6 +141,15 @@ func Creds(c credentials.Credentials) ServerOption { } } +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 // started to accept requests yet. func NewServer(opt ...ServerOption) *Server { @@ -494,7 +504,7 @@ func (s *Server) processUnaryRPC(t transport.ServerTransport, stream *transport. } 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 err, ok := appErr.(rpcError); ok { statusCode = err.code diff --git a/test/end2end_test.go b/test/end2end_test.go index 88f1dce2..73befc2f 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -420,6 +420,7 @@ type test struct { userAgent string clientCompression bool serverCompression bool + unaryInt grpc.UnaryServerInterceptor // srv and srvAddr are set once startServer is called. srv *grpc.Server @@ -468,7 +469,9 @@ func (te *test) startServer() { grpc.RPCDecompressor(grpc.NewGZIPDecompressor()), ) } - + if te.unaryInt != nil { + sopts = append(sopts, grpc.UnaryInterceptor(te.unaryInt)) + } la := "localhost:0" switch e.network { case "unix": @@ -1685,6 +1688,30 @@ 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, // similar to an http.HandlerFunc. // Any unimplemented method will crash. Tests implement the method(s) From 310ca08496121aa6e62d33da072766f8e60bce44 Mon Sep 17 00:00:00 2001 From: iamqizhao Date: Tue, 19 Apr 2016 15:56:17 -0700 Subject: [PATCH 2/3] gofmt -w --- server.go | 2 +- test/end2end_test.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/server.go b/server.go index 995dcaea..f7e674c7 100644 --- a/server.go +++ b/server.go @@ -99,7 +99,7 @@ type options struct { codec Codec cp Compressor dc Decompressor - unaryInt UnaryServerInterceptor + unaryInt UnaryServerInterceptor maxConcurrentStreams uint32 useHandlerImpl bool // use http.Handler-based server } diff --git a/test/end2end_test.go b/test/end2end_test.go index 73befc2f..09b786d3 100644 --- a/test/end2end_test.go +++ b/test/end2end_test.go @@ -1688,7 +1688,6 @@ func testCompressOK(t *testing.T, e env) { } } - func TestUnaryServerInterceptor(t *testing.T) { defer leakCheck(t)() for _, e := range listTestEnv() { From 616cb8ecfe14f14fc5e1d6cb3d9e89cb720b3a4a Mon Sep 17 00:00:00 2001 From: iamqizhao Date: Tue, 19 Apr 2016 16:25:23 -0700 Subject: [PATCH 3/3] Add the missing comment --- server.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server.go b/server.go index f7e674c7..53a2a92c 100644 --- a/server.go +++ b/server.go @@ -141,6 +141,9 @@ 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 {