gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/interceptor.go (about)

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