interceptor.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.
  25. // Unary interceptors can be specified as a DialOption, using
  26. // WithUnaryInterceptor() or WithChainUnaryInterceptor(), when creating a
  27. // ClientConn. When a unary interceptor(s) is set on a ClientConn, gRPC
  28. // delegates all unary RPC invocations to the interceptor, and it is the
  29. // responsibility of the interceptor to call invoker to complete the processing
  30. // of the RPC.
  31. //
  32. // method is the RPC name. req and reply are the corresponding request and
  33. // response messages. cc is the ClientConn on which the RPC was invoked. invoker
  34. // is the handler to complete the RPC and it is the responsibility of the
  35. // interceptor to call it. opts contain all applicable call options, including
  36. // defaults from the ClientConn as well as per-call options.
  37. //
  38. // The returned error must be compatible with the status package.
  39. type UnaryClientInterceptor func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error
  40. // Streamer is called by StreamClientInterceptor to create a ClientStream.
  41. type Streamer func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error)
  42. // StreamClientInterceptor intercepts the creation of a ClientStream. Stream
  43. // interceptors can be specified as a DialOption, using WithStreamInterceptor()
  44. // or WithChainStreamInterceptor(), when creating a ClientConn. When a stream
  45. // interceptor(s) is set on the ClientConn, gRPC delegates all stream creations
  46. // to the interceptor, and it is the responsibility of the interceptor to call
  47. // streamer.
  48. //
  49. // desc contains a description of the stream. cc is the ClientConn on which the
  50. // RPC was invoked. streamer is the handler to create a ClientStream and it is
  51. // the responsibility of the interceptor to call it. opts contain all applicable
  52. // call options, including defaults from the ClientConn as well as per-call
  53. // options.
  54. //
  55. // StreamClientInterceptor may return a custom ClientStream to intercept all I/O
  56. // operations. The returned error must be compatible with the status package.
  57. type StreamClientInterceptor func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error)
  58. // UnaryServerInfo consists of various information about a unary RPC on
  59. // server side. All per-rpc information may be mutated by the interceptor.
  60. type UnaryServerInfo struct {
  61. // Server is the service implementation the user provides. This is read-only.
  62. Server interface{}
  63. // FullMethod is the full RPC method string, i.e., /package.service/method.
  64. FullMethod string
  65. }
  66. // UnaryHandler defines the handler invoked by UnaryServerInterceptor to complete the normal
  67. // execution of a unary RPC. If a UnaryHandler returns an error, it should be produced by the
  68. // status package, or else gRPC will use codes.Unknown as the status code and err.Error() as
  69. // the status message of the RPC.
  70. type UnaryHandler func(ctx context.Context, req interface{}) (interface{}, error)
  71. // UnaryServerInterceptor provides a hook to intercept the execution of a unary RPC on the server. info
  72. // contains all the information of this RPC the interceptor can operate on. And handler is the wrapper
  73. // of the service method implementation. It is the responsibility of the interceptor to invoke handler
  74. // to complete the RPC.
  75. type UnaryServerInterceptor func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error)
  76. // StreamServerInfo consists of various information about a streaming RPC on
  77. // server side. All per-rpc information may be mutated by the interceptor.
  78. type StreamServerInfo struct {
  79. // FullMethod is the full RPC method string, i.e., /package.service/method.
  80. FullMethod string
  81. // IsClientStream indicates whether the RPC is a client streaming RPC.
  82. IsClientStream bool
  83. // IsServerStream indicates whether the RPC is a server streaming RPC.
  84. IsServerStream bool
  85. }
  86. // StreamServerInterceptor provides a hook to intercept the execution of a streaming RPC on the server.
  87. // info contains all the information of this RPC the interceptor can operate on. And handler is the
  88. // service method implementation. It is the responsibility of the interceptor to invoke handler to
  89. // complete the RPC.
  90. type StreamServerInterceptor func(srv interface{}, ss ServerStream, info *StreamServerInfo, handler StreamHandler) error