interceptor.go 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpc
  19. import (
  20. "context"
  21. )
  22. // UnaryInvoker is called by UnaryClientInterceptor to complete RPCs.
  23. type UnaryInvoker func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, opts ...CallOption) error
  24. // UnaryClientInterceptor intercepts the execution of a unary RPC on the client. invoker is the handler to complete the RPC
  25. // and it is the responsibility of the interceptor to call it.
  26. // This is an EXPERIMENTAL API.
  27. type UnaryClientInterceptor func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error
  28. // Streamer is called by StreamClientInterceptor to create a ClientStream.
  29. type Streamer func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error)
  30. // StreamClientInterceptor intercepts the creation of ClientStream. It may return a custom ClientStream to intercept all I/O
  31. // operations. streamer is the handler to create a ClientStream and it is the responsibility of the interceptor to call it.
  32. // This is an EXPERIMENTAL API.
  33. type StreamClientInterceptor func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error)
  34. // UnaryServerInfo consists of various information about a unary RPC on
  35. // server side. All per-rpc information may be mutated by the interceptor.
  36. type UnaryServerInfo struct {
  37. // Server is the service implementation the user provides. This is read-only.
  38. Server interface{}
  39. // FullMethod is the full RPC method string, i.e., /package.service/method.
  40. FullMethod string
  41. }
  42. // UnaryHandler defines the handler invoked by UnaryServerInterceptor to complete the normal
  43. // execution of a unary RPC. If a UnaryHandler returns an error, it should be produced by the
  44. // status package, or else gRPC will use codes.Unknown as the status code and err.Error() as
  45. // the status message of the RPC.
  46. type UnaryHandler func(ctx context.Context, req interface{}) (interface{}, error)
  47. // UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info
  48. // contains all the information of this RPC the interceptor can operate on. And handler is the wrapper
  49. // of the service method implementation. It is the responsibility of the interceptor to invoke handler
  50. // to complete the RPC.
  51. type UnaryServerInterceptor func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error)
  52. // StreamServerInfo consists of various information about a streaming RPC on
  53. // server side. All per-rpc information may be mutated by the interceptor.
  54. type StreamServerInfo struct {
  55. // FullMethod is the full RPC method string, i.e., /package.service/method.
  56. FullMethod string
  57. // IsClientStream indicates whether the RPC is a client streaming RPC.
  58. IsClientStream bool
  59. // IsServerStream indicates whether the RPC is a server streaming RPC.
  60. IsServerStream bool
  61. }
  62. // StreamServerInterceptor provides a hook to intercept the execution of a streaming RPC on the server.
  63. // info contains all the information of this RPC the interceptor can operate on. And handler is the
  64. // service method implementation. It is the responsibility of the interceptor to invoke handler to
  65. // complete the RPC.
  66. type StreamServerInterceptor func(srv interface{}, ss ServerStream, info *StreamServerInfo, handler StreamHandler) error